当RecyclerView的item包含复选框时,未显示涟漪/触摸反馈

7
我希望RecyclerView中的项在被按下时能够有触摸反馈或波纹效果,但它们似乎不起作用,我认为这是由于复选框引起的。
只有在长按时才会显示波纹,而简单的按下则不会显示。
请问有人可以帮助我解决这个问题吗?提前感谢。
附注:我之前使用的是ListView,而且项布局的父布局是LinearLayout。波纹效果正常工作。但是转移到RecyclerView后,项的波纹效果就不起作用了。我尝试重新使用LinearLayout,但仍然不起作用。
以下是布局文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/requestCard"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:maxHeight="64dp"
        android:maxWidth="64dp"
        android:padding="@dimen/lists_padding"
        android:src="@drawable/ic_launcher"
        tools:ignore="ContentDescription"/>

    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/imgIcon"
        android:layout_toRightOf="@+id/imgIcon"
        android:ellipsize="end"
        android:maxLength="@integer/request_text_length"
        android:maxLines="1"
        android:padding="@dimen/lists_padding"
        android:textSize="@dimen/abc_text_size_large_material"
        tools:text="App Name"/>

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:padding="@dimen/lists_padding"/>

</RelativeLayout>

在您的项目的根元素中添加android:descendantFocusability="blocksDescendants"。 - Gabriele Mariotti
@GabrieleMariotti已经完成了。仍然无法工作。更新了布局文件。 - Jahir Fiquitiva
我不太确定,但这可能是因为视图组中的视图遮盖了你的涟漪效果。所以它存在,但你看不到它。所以我最终添加了一个额外的简单视图(宽度和高度都为match_parent),专门用于涟漪效果。你可以试试看。 - DmitryO.
4个回答

9

在您的onClick方法中(假设在回调中),请确保在notifyItemChanged(position)之后不要调用notifyDataSetChanged()

notifyDataSetChanged()会与默认的涟漪效果冲突。

new recyclerAdapter.ClickListener() {
    @Override
    public void onClick(int position) {

        ... awesome item onClick code ...

        notifyItemChanged(position);
        //notifyDataSetChanged(); <//--- Causes the no ripple bug
    }
};

2
如果我需要在项目点击时更新其他可见项目,我该怎么做? - vishal patel

1

我想您正在寻找涟漪效果。我可以给您一个基本的想法,只需在drawable中创建一个xml文件(ripple.xml),并在其中编写以下代码:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white"
        >

    </item>
</ripple>

& 只需在另一个声明了recyclerview元素的xml文件中写入两行即可。

android:background="@drawable/ripple"
android:clickable="true"

就像我的情况一样,我只有一个TextView,在父标签中声明了这两行代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ripple"
    android:clickable="true"
   >

<TextView
    android:id="@+id/textView_Username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="@color/colorPrimary"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    />
</LinearLayout>

最终结果如下:

Finally the result:enter image description here

查看此视频

https://www.youtube.com/watch?v=qvVkDb7DqCk


1
你使用了 android:background="?android:attr/selectableItemBackground",但你应该使用 android:foreground="?android:attr/selectableItemBackground"

我刚刚在测试项目中尝试了您的布局,一切正常 - https://drive.google.com/file/d/0B5KouoPaI5qqQWFRenVpRkRlQ3M/view?usp=sharing - curioushikhov
@curioshikhov,我尝试了相同的布局,但仍然没有结果...有什么地方可以联系你并得到帮助吗?我不能分享完整的源代码,因为它是私有的,但如果你愿意帮忙,我可能会这样做,提前感谢你。 - Jahir Fiquitiva

0
我在使用一种自定义的涟漪设置,将其设置在我的父布局的背景属性上。 android:background="@drawable/bgr_collection_item"
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?colorControlHighlight">
    <!--  the ripple's color (1) -->

    <!--  no `id` so our <shape> will be drawn and not just used as mask -->
    <item>
        <shape>
            <corners android:radius="16dp" />
            <solid android:color="@color/white" />
        </shape>
    </item>

</ripple>

notifyItemChanged(position); 没有禁用涟漪,而是加速了它。

使用 recyclerView.setItemAnimator(null); 允许在调用 notifyItemChanged(position); 时发生原始涟漪动画。

感谢这个答案:https://dev59.com/f1wZ5IYBdhLWcg3wFMoR#43888196


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接