RecyclerView + CardView + Touch feedback

前端 未结 5 1807
一整个雨季
一整个雨季 2021-02-02 10:17

Has anybody solved the mystery of the CardView without touch feedback when it\'s inside a RecyclerView?

I have a RecyclerView with a bu

5条回答
  •  难免孤独
    2021-02-02 11:13

    Both Approach should work the same way.

    1) If you want cardview to respond to touch feedback then use this one in cardview.

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

    but if above approach is not working then you can set this property on child view group (Linear/Relative etc) of cardview.

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

    But then itemView of ViewHolder will not respond to the touch event. since its been consumed by child view so you have to set clicklistener on childview to work on further with listener in recyclerview adapter, this way we can enable touch as well as click events on row items of recyclerview in our adapter.

    if you have hard time following the touch and click on views in cardview with ripple then this might be helpful. Touch Feedback Problem

    2.) The second approach is to use the traditional way of using custom touch selector drawable and set as background.

        
    
        
        
            
                
            
            
                
            
            
                
            
        
        
    
    

    Docs Ripple Drawable

提交回复
热议问题