【问题标题】:RecyclerView item not showing ripples/touch feedback when the item has a checkbox当项目具有复选框时,RecyclerView 项目不显示涟漪/触摸反馈
【发布时间】:2015-12-10 00:06:28
【问题描述】:

我希望 RecyclerView 中的项目在按下时有触摸反馈或涟漪,但它们似乎不起作用,我认为这是因为复选框。

波纹只在长按时显示,但简单按不会显示。

有人可以帮我解决它吗?提前致谢。

PD:我使用的是 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"。
  • @GabrieleMariotti 已经做到了。还是行不通。更新了布局文件。
  • 我不太确定,但这可能会发生,因为视图组中的视图覆盖了您的连锁反应。所以它就在那里,但你看不到它。所以我最终为波纹添加了一个额外的简单视图(具有 match_parent 宽度和高度)。您可能想尝试一下。

标签: android android-layout android-recyclerview


【解决方案1】:

在您的 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
    }
};

【讨论】:

  • 如果在item点击时必须更新其他可见item怎么办?
【解决方案2】:

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

【讨论】:

  • 我刚刚在测试项目中尝试了您的布局,一切正常 - drive.google.com/file/d/0B5KouoPaI5qqQWFRenVpRkRlQ3M/…
  • @curioshikhov 我已经尝试了相同的布局,但仍然一无所获......有什么地方可以联系您并获得您的帮助吗?由于是私人的,我无法分享完整的源代码,但如果你愿意帮助我,我可以这样做,在此先感谢。
【解决方案3】:

我认为您正在寻找涟漪效应。我可以给您基本的想法,只需在 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>

终于出结果了:

观看此视频

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

【讨论】:

    【解决方案4】:

    我在父布局的背景属性上使用自定义波纹设置

    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://stackoverflow.com/a/43888196/11110509

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 2016-11-26
      • 2018-05-10
      • 2015-11-07
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      相关资源
      最近更新 更多