Touch feedback with RecyclerView and CardView

前端 未结 10 751
一向
一向 2020-12-07 09:32

I would love to enable touch feedback for my Open-Source library.

I\'ve created a RecyclerView and a CardView. The CardView co

相关标签:
10条回答
  • 2020-12-07 10:22

    Had the same problem as Jiang:

    I have to set the attributes to the layout-file, which is inflating the RecycleView.

    My Main-Layout: (Don't add the properties here!)

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    My RecycleViewAdapter:

                View v = inflater.inflate(R.layout.list_center_item, parent, false);
    

    My list_center_item.xml (Add the properties here!)

    <?xml version="1.0" encoding="utf-8"?>
    
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    
    
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    />
    
    0 讨论(0)
  • 2020-12-07 10:26

    You have to add following lines in the layout that you are using under CardView

    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/my_ripple"
    

    Example

    <android.support.v7.widget.CardView android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/my_ripple">
    
    0 讨论(0)
  • 2020-12-07 10:30

    Assuming you are using Material/Appcompat theme and Lollipop,I got this to work by making the CardView have the following attributes:

    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    
    0 讨论(0)
  • 2020-12-07 10:30

    None of the above answers worked for me or was either too complicated.

    Then I realized I had to set the following properties in THE RECYCLERVIEW ADAPTER LAYOUT.

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

    Then I got it to work.

    0 讨论(0)
提交回复
热议问题