RecyclerView项未显示涟漪/触感反馈时,该项目都有一个复选框 [英] RecyclerView item not showing ripples/touch feedback when the item has a checkbox

查看:864
本文介绍了RecyclerView项未显示涟漪/触感反馈时,该项目都有一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在RecyclerView的项目有触摸反馈或涟漪,当pressed,但他们似乎没有工作,我认为这是因为复选框。

纹波时,才会显示长时pressing,但一个简单的preSS不会表现出来。

有人可以帮我解决?先谢谢了。

PD:我用一个ListView,且项目布局父是一个LinearLayout中。涟漪都工作正常。移动到RecyclerView后,物品的波纹不工作。我用的LinearLayout再次尝试,但仍然没有工作。

这里的布局文件

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
    机器人:ID =@ + ID / requestCard
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=机器人:ATTR / selectableItemBackground
    机器人:可点击=真
    机器人:可聚焦=真
    机器人:descendantFocusability =blocksDescendants
    机器人:方向=横向>    < ImageView的
        机器人:ID =@ + ID / imgIcon
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:adjustViewBounds =真
        安卓了maxHeight =64dp
        安卓了maxWidth =64dp
        机器人:填充=@扪/ lists_padding
        机器人:SRC =@绘制/ ic_launcher
        工具:忽略=ContentDescription/>    <的TextView
        机器人:ID =@ + ID / txtName的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:layout_toEndOf =@ + ID / imgIcon
        机器人:layout_toRightOf =@ + ID / imgIcon
        机器人:ellipsize =结束
        机器人:最大长度=@整数/ request_text_length
        机器人:MAXLINES =1
        机器人:填充=@扪/ lists_padding
        机器人:TEXTSIZE =@扪/ abc_text_size_large_material
        工具:文本=应用程序名/>    <复选框
        机器人:ID =@ + ID / chkSelected
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentEnd =真
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真
        机器人:可点击=假
        机器人:填充=@扪/ lists_padding/>< / RelativeLayout的>


解决方案

在您的onClick方法(在你的回调假设),请确保您没有拨打 notifyDataSetChanged() notifyItemChanged(位置)


  

notifyDataSetChanged()将与默认的纹波发生冲突
  的效果。


 新recyclerAdapter.ClickListener(){
    @覆盖
    公共无效的onClick(INT位置){        ...真棒项目的onClick code ...        notifyItemChanged(位置);
        // notifyDataSetChanged(); < // ---原因无波纹的bug
    }
};

I want the items in a RecyclerView to have touch feedback or ripples when pressed, but they seem to not be working, and I think it's because of the checkbox.

The ripple is only shown when long pressing, but a simple press won't show it.

May someone help me to fix it? Thanks in advance.

PD: I was using a ListView, and the item layout parent was a LinearLayout. The ripples were working fine. After moving to RecyclerView, the items ripples don't work. I tried with the LinearLayout again, but still not working.

Here's the layout file

<?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>

解决方案

In your onClick method (assuming in your callback), make sure you do not call notifyDataSetChanged() after notifyItemChanged(position).

notifyDataSetChanged() will conflict with those default ripple effects.

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

        ... awesome item onClick code ...

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

这篇关于RecyclerView项未显示涟漪/触感反馈时,该项目都有一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆