Android 在listview的元素中分别设置文本视图的动画

Android 在listview的元素中分别设置文本视图的动画,android,listview,animation,Android,Listview,Animation,我有一个布局,它由多个视图组成,这些视图排列在一个相对的布局中。此布局将在listview的适配器中膨胀 现在,我想分别设置listview的每个元素的动画,不是元素本身,而是其中的textview,例如在onClick事件上。动画应该包括文本视图的过渡和另一个文本视图的淡入 我真的不知道。我有一个对象的ArrayList,它保存listview元素的数据。此ArrayList提供给填充TextView的适配器 我想我现在需要一种方法来接近文本视图。但是我该怎么做呢?我摆弄了适配器类的getVi

我有一个布局,它由多个视图组成,这些视图排列在一个相对的布局中。此布局将在listview的适配器中膨胀

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

我真的不知道。我有一个对象的ArrayList,它保存listview元素的数据。此ArrayList提供给填充TextView的适配器

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

我尝试在适配器中附加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制作动画。

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

然后在onClick过程中,你得到一个对它的引用并启动它


希望有帮助。

我想这是基于观点的。尽管我不是android专家,但我相信无论您选择何种方法,您仍然需要获取ListView中每个TextView的引用,指定OnClickListener并启动动画。因此,我将以这样一种方式来处理这个问题,即通过创建一个自定义视图来扩展TextView类,在该类中我将定义与此TextView相关的所有内容,从而使我的活动/片段不受子视图特定逻辑的污染。因此,基本上,活动/片段只需要知道ListView如何…它不关心其中的项目或项目正在做什么…这是从特定于体系结构的角度来看的

@mazen..你能给我们一些代码以便我们研究它吗..这对你有帮助吗..我会在适配器中这样做,对吗?我的问题仍然是,我无法获得视图的引用。我认为它应该与Adapter.getView一起使用。我想我再试一次,然后发布结果。在适配器中,你使用setTag,然后在单击中,你有一个位置,你可以使用适配器获取视图。我可能错过了这里显而易见的位置。但是我如何从适配器中获取对特定元素的文本视图的引用?我知道它的位置。谢谢。我试过了,但动画似乎不起作用。见我上面编辑的问题
  // Animate X from 0 to 200
  ObjectAnimator animation2 = ObjectAnimator.ofFloat(myview,"x", 200);
  animation2.setDuration(2000);
  myview.setTag(animation2)
 ((ObjectAnimator ) myview.getTag()).start()