Touch feedback with RecyclerView and CardView

前端 未结 10 750
一向
一向 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:05

    The actual silly issue was in my case is the following....

    Technically, It's not the case just like everyone suggests here in my case. The real issue is you should not be using android:clickable="true" uncesseriliy everywhere in the LayoutView, RelativeView or TextView.

    It should only be in CardView. Then it works like charm

    android:focusable="true" Is not really requied.

    Just add the following in your CardView

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


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="@dimen/card_margin"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:elevation="3dp"
            card_view:cardCornerRadius="@dimen/card_corner_radius">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:id="@+id/card_template_item_image"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/card_size_height"
                    android:scaleType="fitXY" />
    
                <TextView
                    android:id="@+id/card_template_item_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/card_template_item_image"
                    android:paddingLeft="@dimen/card_title_padding"
                    android:paddingRight="@dimen/card_title_padding"
                    android:paddingTop="@dimen/card_title_padding"
                    android:textSize="@dimen/card_title_size"
                    android:text="@string/place_holder_item_name"/>
    
                <TextView
                    android:id="@+id/card_template_item_sub_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/card_template_item_title"
                    android:paddingBottom="@dimen/card_sub_title_padding"
                    android:paddingLeft="@dimen/card_sub_title_padding"
                    android:paddingRight="@dimen/card_sub_title_padding"
                    android:textSize="@dimen/card_sub_title_size"
                    android:text="@string/place_holder_item_category"/>
    
            </RelativeLayout>
    
        </android.support.v7.widget.CardView>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-07 10:06

    I was now able to solve the issue.

    The problem is that i use onClickListener on the TextViews and this click listener prevents the RippleForeground from being notified about the touch event. So the solution is to implement an TouchListener on those TextViews and pass through the touch event.

    The class is really simple, and can be used everywhere:

    package com.mikepenz.aboutlibraries.util;
    
    import android.support.v7.widget.CardView;
    import android.view.MotionEvent;
    import android.view.View;
    
    /**
     * Created by mikepenz on 16.04.15.
     */
    public class RippleForegroundListener implements View.OnTouchListener {
        CardView cardView;
    
        public RippleForegroundListener setCardView(CardView cardView) {
            this.cardView = cardView;
            return this;
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Convert to card view coordinates. Assumes the host view is
            // a direct child and the card view is not scrollable.
            float x = event.getX() + v.getLeft();
            float y = event.getY() + v.getTop();
    
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                // Simulate motion on the card view.
                cardView.drawableHotspotChanged(x, y);
            }
    
            // Simulate pressed state on the card view.
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    cardView.setPressed(true);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    cardView.setPressed(false);
                    break;
            }
    
            // Pass all events through to the host view.
            return false;
        }
    }
    

    It can be used by adding this as TouchListener:

    RippleForegroundListener rippleForegroundListener = new RippleForegroundListener();
    older.libraryCreator.setOnTouchListener(rippleForegroundListener);
    

    This listener will just pass through the touch event to the CardView and trigger the correct Ripple effect. (You can also modify this to take any view. It should not be limited to a CardView)

    0 讨论(0)
  • 2020-12-07 10:07

    CardLayout is just a subclass of ViewGroup, So setting:

    android:background="?android:attr/selectableItemBackground"
    

    should do the trick.

    UPDATE:

    (looks like you are trying to use a custom ripple color.)

    Try using a ripple drawable with custom color as the background (I haven't used this, So I cannot verify this behavior.) see: the documentation or this question to get you started.

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

    If you use foreground instead background, you don't need to use clickable or focusable

    0 讨论(0)
  • 2020-12-07 10:10

    In my case, i was need to show custom background and ripple effect as well. So the way I achieved this is by applying these two attributes on the root layout of my recycler-view item:

    android:background="@drawable/custom_bg"
    android:foreground="?android:attr/selectableItemBackground"
    
    0 讨论(0)
  • 2020-12-07 10:16

    for me:

    android:background="?android:attr/selectableItemBackground"
    

    made it finally work. see background of recyclerView / content behind the item, and also see the ripple effect used with recyclerView.

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