【问题标题】:Animate Textviews within elements of a listview separately分别在列表视图的元素中为 Textview 设置动画
【发布时间】:2014-08-08 09:06:40
【问题描述】:

我有一个布局,其中包含以相对布局排列的多个视图。此布局将在列表视图的适配器内膨胀。

现在我想分别为列表视图的每个元素设置动画(不是元素本身,而是其中的一个文本视图),例如在 onClick 事件上。动画应该包含一个文本视图的过渡和另一个文本视图的淡入淡出。

我真的不知道这里。我有我的 ArrayList of Objects,它保存了我的列表视图元素的数据。这个 ArrayList 被提供给填充文本视图的适配器。

我想我现在需要一种方法来处理文本视图。但是我该怎么做呢?我摆弄了 Adapter 类的 getView 方法,但没有任何成功。在适配器/数组列表中获取被点击元素的位置完全没有问题。 有什么想法/不同的方法吗?

我尝试在我的适配器中附加一个 ObjectAnimator,如下所示:

ObjectAnimator animation = ObjectAnimator.ofFloat(holder.txtPlayerPoints,"x", 200);
animation.setDuration(2000);
holder.txtPlayerPoints.setTag(R.id.tag_animation_in,animation);

在我的 onClick 中,我尝试像这样启动它:

((ObjectAnimator ) textView.getTag(R.id.tag_animation_in)).start();

对 textView 的引用是正确的,但它没有移动。调试器还说我的 textView 的“mTag”属性是“null”。有什么建议吗?

编辑:这是每个元素的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="@dimen/bar_upper_height"
    android:paddingLeft="@dimen/bar_name_padding"
    android:text="Hendrik"
    android:textSize="@dimen/font_player_name"
    android:id="@+id/txtNamePlayer"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@color/txt_default"
    android:textStyle="bold"
    android:textColor="@android:color/white"
    android:gravity="center_vertical" />

<TextView
    android:layout_width="@dimen/bar_rank_width"
    android:layout_height="@dimen/bar_rank_height"
    android:layout_marginLeft="@dimen/bar_upper_margin"
    android:layout_marginBottom="@dimen/bar_upper_margin"
    android:textSize="@dimen/font_bar_info"
    android:text="1"
    android:id="@+id/txtRankPlayer"
    android:layout_alignLeft="@id/txtNamePlayer"
    android:layout_alignBottom="@id/txtNamePlayer"
    android:background="@android:color/white"
    android:textStyle="bold"
    android:textColor="@color/txt_default"
    android:gravity="center_vertical|center_horizontal" />

<ImageView
    android:layout_width="@dimen/bar_arrow_width"
    android:layout_height="@dimen/bar_rank_height"
    android:id="@+id/imgActivePlayer"
    android:layout_toRightOf="@id/txtRankPlayer"
    android:layout_marginLeft="@dimen/bar_upper_margin"
    android:layout_alignBottom="@id/txtNamePlayer"
    android:layout_marginBottom="@dimen/bar_upper_margin"
    android:background="@drawable/active_player" />

<TextView
    android:layout_width="@dimen/bar_rank_height"
    android:layout_height="@dimen/bar_rank_height"
    android:layout_marginRight="@dimen/bar_upper_margin"
    android:layout_marginBottom="@dimen/bar_upper_margin"
    android:text="12"
    android:id="@+id/txtPointsPlayer"
    android:layout_alignRight="@id/txtNamePlayer"
    android:layout_alignBottom="@id/txtNamePlayer"
    android:background="@android:color/white"
    android:textStyle="bold"
    android:textColor="@color/txt_default"
    android:textSize="@dimen/font_bar_info"
    android:gravity="center_vertical|center_horizontal" />

<TextView
    android:layout_width="@dimen/bar_rank_height"
    android:layout_height="@dimen/bar_rank_height"
    android:layout_marginRight="@dimen/bar_upper_margin"
    android:layout_marginBottom="@dimen/bar_upper_margin"
    android:layout_toLeftOf="@id/txtPointsPlayer"
    android:layout_alignBottom="@id/txtNamePlayer"
    android:text="0"
    android:id="@+id/txtCurrScorePlayer"
    android:background="@color/txt_disabled"
    android:textColor="@android:color/white"
    android:textSize="@dimen/font_bar_info"
    android:gravity="center_vertical|center_horizontal" />

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/aheadBehindPlayer"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/bar_lower_height"
    android:layout_below="@+id/txtNamePlayer"
    android:layout_centerHorizontal="true"
    android:background="@color/txt_disabled"
    android:gravity="center_vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ahead"
        android:textSize="@dimen/font_bar_info"
        android:textColor="@android:color/white"
        android:id="@+id/textView2"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="15"
        android:textSize="@dimen/font_bar_info"
        android:textColor="@android:color/white"
        android:id="@+id/txtPlayerAhead"
        android:layout_weight="5"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="15"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_bar_info"
        android:id="@+id/txtPlayerBehind"
        android:textStyle="bold"
        android:gravity="right"
        android:layout_weight="5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/behind"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_bar_info"
        android:id="@+id/textView5"
        android:gravity="right"
        android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>

我想为 txtPointsPLayer 和 txtCurrScorePlayer 制作动画。

【问题讨论】:

标签: android listview animation


【解决方案1】:

只需将 ObjectAnimator 附加到每个视图(使用 setTag()),然后稍后在 onClick 期间检索它。要创建动画师,您可以执行以下操作:

  // Animate X from 0 to 200
  ObjectAnimator animation2 = ObjectAnimator.ofFloat(myview,"x", 200);
  animation2.setDuration(2000);
  myview.setTag(animation2)

然后在 onClick 期间,您会获得对它的引用并启动它

 ((ObjectAnimator ) myview.getTag()).start()

希望对你有帮助。

【讨论】:

  • 我会在适配器中执行此操作,对吗?我的问题仍然是,我无法获得对视图的引用。我认为它应该与 Adapter.getView 一起使用。我想我再试一次并发布结果。
  • 在适配器中你使用“setTag()”然后点击你有一个“位置”,你可以使用适配器来取回视图
  • 我可能错过了这里的明显内容。但是如何从我的适配器中获取对特定元素(我知道它的位置)的 textview 的引用?
  • 谢谢。我试过了,但动画似乎不起作用。 (见上面我编辑的问题)
【解决方案2】:

我想这几乎是基于意见的。即使我不是安卓专家,我相信无论您选择哪种方法,您仍然需要在ListView 中获取每个TextView 的引用,指定OnClickListener 并启动动画。因此,我会以这样一种方式处理这个问题,即通过创建一个从 TextView 类扩展的自定义视图,我不会让我的活动/片段被子视图特定的逻辑污染,我将在其中定义与此相关的所有内容TextViews。所以,基本上,activity/fragment 只需要知道ListView...它不关心其中的项目或项目在做什么...这是从特定于架构的角度来看的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多