1

在这种情况下,问题与 RecyclerView 相关,我希望涟漪效应从手指触摸自定义颜色的位置到视图的全部范围进行动画处理(大致如果不完全),就像列表视图中的行一样根据视图的布局,最后以另一种颜色选择的行结束其动画。

我有

items_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rowLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingBottom="5dp"
  android:paddingTop="5dp"
  android:background="@drawable/items_ripple_state_selector"
  >

  <RelativeLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center">

    <TextView
      android:id="@+id/item_unit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/item_amount"
      android:layout_centerHorizontal="true"
      android:gravity="center"
      android:text="unit" />

    <TextView
      android:id="@+id/item_amount"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:gravity="center"
      android:text="amount" />
  </RelativeLayout>

  <RelativeLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4.6"
    android:gravity="center_vertical">

    <TextView
      android:id="@+id/item_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="title"
      android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
      android:id="@+id/item_description"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/item_title"
      android:layout_alignWithParentIfMissing="true"
      android:layout_below="@+id/item_title"
      android:text="description" />
  </RelativeLayout>
</LinearLayout>

items_ripple_state_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <selector>
      <item android:state_pressed="true" android:drawable="@color/state_activated" />
      <item android:state_selected="true" android:drawable="@color/state_selected" />
      <item android:drawable="@android:color/transparent" />
    </selector>
  </item>
  <item>
    <ripple android:color="@color/primary">
      <item android:id="@android:id/mask">
        <color android:color="@android:color/white" />
      </item>
    </ripple>
  </item>
</layer-list>

结果不好的测试

我已经在以下设备上进行了测试,其中涟漪效果动画过早停止,导致更多的是“blip”/“ping”而不是视图边缘的完全扩展。

  • Nexus 6、牛轧糖 (7.0.0)、API 24
  • Nexus 5X,Nougat (7.xx),次要版本不是 100%
  • 不同的 AVD 针对 API 级别 24

结果很好的测试

我已经在以下设备上进行了测试,其中涟漪效果动画以我期望的全长动画精美地呈现到视图的边缘:

  • Nexus 5,棉花糖 (6.0.1),API 级别 23
  • Nexus 4,棒棒糖 (5.1.1),API 级别 22

我一生都无法弄清楚这是怎么回事。当动画中途停止时,在 N6 和 N5X 上看起来非常可怕,动画何时停止没有明显的模式。

我已经阅读了很多 SO 问题以及 RecyclerView、StateListDrawable 和 RippleDrawable 文档,但没有任何内容可以解释我看到的行为。

我最接近的,这是我目前正在使用的,以及我在这篇文章中分享的代码,来自这个答案https://stackoverflow.com/a/31335539/975641

有谁知道为什么会发生这种情况以及如何解决它?

4

1 回答 1

1

我不知道它在 Marshmallow 中起作用的具体原因,但在 Nougat 中不起作用。但是,最常见的是当您看到此问题时,这是因为RecyclerView刷新和/或重新绑定方式太多。

基本上会发生什么

  1. 用户点击
  2. 动画开始
  3. 某事要求对该项目重新绑定
  4. RecyclerView 在项目上调用 rebind
  5. 由于视图重置,动画被取消。

最常见的情况是调用任一RecyclerAdapter#notifyItemChanged(int)方法或调用RecyclerAdapter#notifyDatasetChanged()次数过多时。

如果可能,最好使用这些RecyclerView.Adapter#notifyItem____方法。notifyDatasetChanged()将导致重新绑定列表中的所有项目。使用这些notifyItem_____方法将确保仅在需要时更新单个项目。否则,您需要确保仅nofityDatasetChanged()在数据集中有实际更改时才调用。

另一个常见问题是,如果应用invalidate程序RecyclerView调用RecyclerView. 这将导致整个视图树的刷新,这将需要重新绑定 a 中的所有项目RecyclerView

于 2017-02-22T22:24:28.893 回答