Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从RecyclerView获得接触反馈?_Java_Android_Android Listview_Android Recyclerview - Fatal编程技术网

Java 如何从RecyclerView获得接触反馈?

Java 如何从RecyclerView获得接触反馈?,java,android,android-listview,android-recyclerview,Java,Android,Android Listview,Android Recyclerview,我实现了一个RecyclerView,但我不知道如何获得触摸反馈(它的连锁反应) 以下是我为OnClick Listener所做的: holder.itemView.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //start Intent } }); 我在XML中添加了可点击和可聚焦。这就是“

我实现了一个
RecyclerView
,但我不知道如何获得触摸反馈(它的连锁反应)

以下是我为OnClick Listener所做的:

holder.itemView.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            //start Intent
        }

    });
我在XML中添加了可点击和可聚焦。这就是“回收者”视图显示的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:padding="4dp" >

您必须将ripple drawable设置为背景:

android:background="@drawable/ripple"
ripple.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffa0a0a0"/>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/recyclerview_item_background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackgroundBorderless">
       ...
    </LinearLayout>
</LinearLayout>
或者,如果不需要矩形遮罩:

android:background="?attr/selectableItemBackgroundBorderless"

这是兼容的前棒棒糖设备,将我们一个简单的选择。我相信这会在具有深色主题的应用程序中产生浅色涟漪,反之亦然,因此,如果你想要定制彩色涟漪,你仍然需要创建一个可绘制的涟漪。

除了@Longi的答案之外:如果你想让你的RecyclerViews的项目有自己的背景,同时产生涟漪效应,您可以按照下面的示例所示进行操作: recycler\u view\u item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffa0a0a0"/>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/recyclerview_item_background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackgroundBorderless">
       ...
    </LinearLayout>
</LinearLayout>

...
在本例中,@drawable/recyclerview\u item\u background是一个九补丁png,但您可以在这里使用任何背景


在我使用android:background=“?attr/selectableItemBackground”时,RecyclerView项的根线性布局确实具有涟漪效应,但子线性布局的背景重叠,因此隐藏了涟漪效应。

在您提供的代码中,这是RelativeLayout not RecyclerView。这就是“回收者视图”中膨胀的项目。我查看了那个链接,但找不到导致触摸反馈的原因。点击效果很好,只是触摸反馈啊,然后你必须设置一个rippledrawable作为相对物的背景。你是怎么做到的?我猜我是问这个问题的。它是否也适用于姜饼设备和其他设备?对于其他看到这一点的人,此链接可能也会对您有所帮助:
android:background=“?attr/selectableitemcondground”
。这正是我想在我的
RecyclerView
中显示的简单触摸反馈。根据您的配置,您可能需要添加关键词“android”:
?android:attr/selectableItemBackground