簡體   English   中英

Recyclerview項目長按漣漪效應

[英]Recyclerview item long click ripple effect

我試圖將Ripple Effect添加到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我如何解決此問題? 謝謝

您需要在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作為選擇器可繪制對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM