【问题标题】:Recyclerview item long click ripple effectRecyclerview 项目长点击涟漪效果
【发布时间】:2018-05-10 16:35:36
【问题描述】:

我正在尝试将波纹效果添加到 RecyclerView 的项目长按。我在网上看了一下,但找不到我需要的东西。我已尝试将 android:background 属性设置为 RecyclerView 本身并将其设置为“?android:selectableItemBackground”,但它不起作用 这是我的来源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="vertical">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"

    >
    <LinearLayout
        android:id="@+id/rightlayout"
        android:layout_width="85dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/u_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:maxLines="1"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:textColor="#808080"
            android:textSize="@dimen/u_common_text_size_small_v1" />


        <TextView
            android:id="@+id/u_amount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:maxLines="1"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:textColor="#000000"
            android:textSize="@dimen/u_common_text_size" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/rightlayout"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/u_common_margin_left"
        android:layout_marginRight="85dp"

        android:orientation="vertical">

        <TextView
            android:id="@+id/u_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:text="teee"
            android:textColor="#808080"
            android:textSize="@dimen/u_common_text_size_small_v1" />


        <TextView
            android:id="@+id/u_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:text="teee"
            android:textColor="#000000"
            android:textSize="@dimen/u_common_text_size" />
    </LinearLayout>


</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/listview_dividerHeight"
    android:background="#cccccc" />

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/customTabLayout">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />
</android.support.v4.widget.NestedScrollView>

这是一个recyclerView适配器的代码sn-p

transactionHolder.mainLayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (onclickListerner != null)
                        onclickListerner.onLongClickListener("", position);

                    return true;
                }
            });

            transactionHolder.mainLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onclickListerner != null)
                        onclickListerner.onClickListener("enter", position);
                }
            });

正如我所说,我尝试使用这个背景

    android:background="?attr/selectableItemBackground"

但我想要一些自定义背景颜色,例如 $f2f2f2 我该如何解决这个问题? 谢谢

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    您需要在 drawablev21 文件夹中创建一个可绘制的波纹。

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="?android:colorControlHighlight"
    tools:targetApi="lollipop">
    
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@color/yourcustomcolor" />
            <stroke
                android:width="@dimen/seperator_size"
                android:color="@color/yourcustomcolor" />
            <corners android:radius="@dimen/rounded_radius" />
        </shape>
    </item>
    

    并为低于 21 的选择器创建其等效的可绘制对象。

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2018-02-23
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多