【问题标题】:Android Recycler View Ripple Effect not workingAndroid Recycler View Ripple Effect 不起作用
【发布时间】:2016-11-14 11:53:14
【问题描述】:

我使用以下 xml drawable 作为我的 recyclerview 列表项的背景。

touch_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"
        android:drawable="@color/green_text_500"/>
    <!-- Default, "just hangin' out" state. The base state also
         implements the ripple effect. -->
    <item android:drawable="@drawable/touch_selector_base" />
</selector>

touch_selector_base.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_grey">
    <item android:id="@android:id/mask" android:drawable="@color/light_grey" />
    <item android:drawable="@color/dark_grey"/>
</ripple>

在列表项中,我在 list_item_quote.xml

中使用 touch_selector.xml,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="86dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/touch_selector"
    >
    // layout
</LinearLayout>

类似我还有另一个 xml drawable touch_selector_dark.xmltouch_selector_base_dark.xml

在我的 recycler-view-adapter 中,我根据索引在这两个可绘制对象之间交替。对于偶数索引,我使用较暗的可绘制对象,对于奇数索引,我使用较浅的背景来产生交替效果。但问题是涟漪效应不起作用。

颜色如下:

light_grey = #5b5b5b

dark_grey = #212121

green_text_500 = #37863a

【问题讨论】:

标签: android android-recyclerview rippledrawable


【解决方案1】:

在你的 recycleview 列表布局中使用这一行

android:background="?android:attr/selectableItemBackground"

【讨论】:

    【解决方案2】:

    试试这个 为我工作

    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground"
    

    【讨论】:

      【解决方案3】:

      我认为您的波纹文件无效,您可以检查一下:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_pressed="true">
              <shape android:shape="rectangle">
                  <solid android:color="@color/dark_grey"/>
              </shape>
          </item>
      </selector>
      

      您可以使用android:foreground="@drawable/touch_selector" 显示波纹效果,也可以使用android:background="" 设置任何其他可绘制背景。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-11
        • 1970-01-01
        相关资源
        最近更新 更多