Recyclerview项目长按涟漪效果

时间:2017-11-27 12:23:00

标签: android android-layout android-recyclerview recycler-adapter

我正在尝试将循环效果添加到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适配器的代码片段

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 我怎么能解决这个问题? 感谢

1 个答案:

答案 0 :(得分: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>

并创建等效的drawable,低于21作为选择器drawable。

相关问题