【问题标题】:Unable to produce ripples on RecyclerView's item (CardView) [duplicate]无法在 RecyclerView 的项目(CardView)上产生涟漪 [重复]
【发布时间】:2016-02-25 16:11:56
【问题描述】:

我正在使用RecyclerView,其项目为CardView。我将卡片视图的背景设为android:background="?android:attr/selectableItemBackground"

我尝试设置foreground,尝试将LinearLayout 作为父级并设置其背景等,但没有任何效果。

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <!--TextView-->
        <!--Imageview-->
        <!--Textview-->
   </RelativeLayout>
</android.support.v7.widget.CardView>

为什么会这样?

编辑:CardView 有一个 onClickListener 可以工作。

编辑 2:我将 RelativeLayout 添加为 CardView 的父级。并使其可点击并给它一个可选择的项目背景。还是没有涟漪。

 <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="?android:attr/selectableItemBackground"
      android:clickable="true"
      android:focusable="true">

      <android.support.v7.widget.CardView
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          card_view:cardCornerRadius="2dp"
          card_view:cardElevation="2dp">

          <!-- ... -->

      </android.support.v7.widget.CardView>
</RelativeLayout>

编辑 3:我发布了我的确切布局代码结构。我没有提到数据绑定部分。但我希望这不会阻止涟漪。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
         <import type="android.view.View"/>
         <variable
             name="viewModel"
             type="com.xxx.xxx.viewmodel.genericviewmodel" />
    </data>


    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="2dp">

        <RelativeLayout
            android:id="@+id/c_open_case_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">

            <!--TextView-->
            <!--Imageview-->
            <!--Textview-->
            <!--TextView-->
            <!--Imageview-->
            <!--Textview-->
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</layout>

【问题讨论】:

  • 你从哪里得到这个布局?我的意思是来自layout资源文件夹
  • 将其设置为可点击和可聚焦。
  • @MD :是的,我是从布局资源文件夹中获取的。
  • @GergelyKőrössy :我已经尝试过可点击和可聚焦的东西。没有任何效果..
  • 你用的是什么安卓版本?

标签: android android-cardview


【解决方案1】:

更新:经过所有尝试重现您的问题,我认为CardViews 或数据绑定没有问题。如果您对答案不满意,您可以随时submit a bug report 给 Android 开发团队。


使用android:foreground 并确保CardView 是可点击的:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp">

    <!-- ... -->

</android.support.v7.widget.CardView>

【讨论】:

  • @Vujasin manojilovic:不起作用
  • @Ashwin 您使用的是哪个版本的 Android?我在我的应用程序中使用此方法并且它有效。您是否尝试过实现自定义 RippleDrawable
  • 马诺伊洛维奇:棉花糖
  • @Ashwin 我也在 Marshmallow 上,无法重现您的问题。我正在使用问题中的布局(CardView > RelativeLayout > TextViews 和 ImageView)。见this screenshot。问题出在其他地方。如果您想向您发送示例代码,请联系我。
  • Manojlovic : 是的,我想看看你的代码。
猜你喜欢
  • 2016-11-26
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2022-11-29
相关资源
最近更新 更多