-1

我正在使用 RecyclerView + GridLayout(3 列)。现在,为了让网格的每个方格更“响应”,我希望每个方格都显示某种分隔线,并且用户点击的每个方格内都会产生涟漪效应。

编辑:我添加了android:foreground="?attr/selectableItemBackground",但没有任何反应。这是现在的单项 xml 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="6dip" >

    <ImageView
        android:foreground="?attr/selectableItemBackground"
        android:id="@+id/icon"
        android:layout_width="128dp"
        android:layout_height="118dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginTop="12dp"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher_background" />
</RelativeLayout>

这是它目前的样子: NoRippleDivider

这就是我希望它看起来的样子: 在此处输入图像描述

这是修复后当前的工作方式,添加后:

android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"

在此处输入图像描述

4

3 回答 3

2

如果您希望对其进行自定义,您可以像这样创建一个可绘制的波纹:

波纹.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimaryLight" />
        <!--<corners android:radius="@dimen/button_radius_large" />-->
    </shape>
</item>

<item android:id="@android:id/background">
    <shape android:shape="rectangle">
        <gradient
            android:angle="90"
            android:endColor="@color/background"
            android:startColor="@color/background"
            android:type="linear" />
        <!--<corners android:radius="10dp" />-->
    </shape>
</item>

并在您的布局中使用它,如下所示:

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

或者你可以简单地这样做:

android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
于 2018-11-20T08:52:47.940 回答
0

您可以通过将 GridItemDecoration 添加到您的 recyclerview 来添加分隔符。我建议你使用这个

单击项目时添加波纹的一种简单方法是将具有 selectableItemBackground 值的前景添加到项目的视图中:

android:foreground="?attr/selectableItemBackground"
于 2018-11-19T20:42:35.503 回答
0

尝试这种方式在 xml 中获得涟漪效应

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="6dip"
        android:clickable="true"
        android:focusable="true"
        android:background="?attr/selectableItemBackground">

        <ImageView

            android:id="@+id/icon"
            android:layout_width="128dp"
            android:layout_height="118dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginTop="12dp"
            android:contentDescription="TODO"
            android:src="@drawable/ic_launcher_background" />
    </RelativeLayout>

并按照此链接查看项目之间 的分隔符如何在 RecyclerView 中的项目之间添加分隔符和空格?

于 2018-11-20T09:11:44.247 回答