Волновой эффект Cardview не работает

Минимальный SDK - 21. Когда я нажимаю на карточку в своем адаптере ресайклера, эффект пульсации не возникает и просто переходит к следующему экрану. Recyclerview находится внутри фрагмента.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/entire_card"
        android:layout_width="match_parent"
        android:layout_height="265dp"
        android:layout_margin="8dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="2dp"
        >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ProgressBar
                android:id="@+id/progressbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_centerHorizontal="true"
                android:paddingTop="120dp"
                android:visibility="visible"/>


            <ImageView
                android:id="@+id/pet_image"
                android:layout_width="match_parent"
                android:layout_height="210dp"
                android:layout_alignParentTop="true"
                android:scaleType="centerCrop"
                android:src="@drawable/placeholder"
                android:visibility="gone"
                 />

            <TextView
                android:id="@+id/pet_description"
                android:layout_width="fill_parent"
                android:layout_height="55dp"
                android:layout_below="@+id/pet_image"
                android:padding="10dp"
                android:textColor="#FFFFFF"
                android:visibility="gone"
                android:textSize="20sp"
                android:background="@color/primaryColour"
                />

        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

Код адаптера, в котором у меня есть onClick для каждого элемента.

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView petInfo;
        public ImageView imgURL;
        public ProgressBar progressBar;

        private Context itemContext;
        public ViewHolder(View v){
            super(v);
            imgURL = (ImageView) v.findViewById(R.id.pet_image);
            petInfo = (TextView) v.findViewById(R.id.pet_description);
            progressBar = (ProgressBar) v.findViewById(R.id.progressbar);

            itemContext = v.getContext();

            v.setOnClickListener(this);
        }

        @Override
        public void onClick(View v){

            Intent intent= new Intent(itemContext, DetailCardLayout.class);
            Integer position = getAdapterPosition();
            intent.putExtra("CARDVIEW_POSITION", position);
            v.getContext().startActivity(intent);

        }
    }

person DessertsAndStuff    schedule 18.02.2017    source источник
comment
Установите CardView в качестве корневого макета и попробуйте.   -  person Surender Kumar    schedule 18.02.2017
comment
Что помогает! Однако волновой эффект работает только для двух кликов. т. е. нажмите карточку, перейдите к следующему экрану и нажмите «Назад». повторить. затем, когда вы нажимаете в третий раз, эффект ряби больше не возникает   -  person DessertsAndStuff    schedule 18.02.2017


Ответы (5)


Ваше представление карты содержит относительный макет, который покрывает поверхность представления карты, поэтому напишите эффект ряби в относительном макете.

<android.support.v7.widget.CardView 
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardPreventCornerOverlap="false"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="false"
    app:cardCornerRadius="2dp"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginLeft="8dp"
    >
    <RelativeLayout
        android:id="@+id/rl_bookmark"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> ...
person Inam Ur Rehman    schedule 05.10.2017

Попробуйте добавить

android:clickable="true"

на вашу карточку просмотра xml

person ATEF    schedule 18.02.2017
comment
Кажется, это помогает. Однако, когда я прокручиваю, чтобы увидеть больше (2 карты занимают экран), кажется, что эффект пульсации не работает после первых двух карт. - person DessertsAndStuff; 18.02.2017

Попробуйте обернуть изображение в FrameLayout с атрибутом переднего плана: android:foreground="?attr/selectableItemBackground"

E.g.

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?attr/selectableItemBackground"
        >

        <ImageView
            android:id="@+id/pet_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            android:src="@drawable/placeholder"
            android:visibility="gone"
             />

    </FrameLayout>
person behelit    schedule 14.03.2017

Другой способ решить эту проблему: использование стиля кнопки.

Добавьте стиль кнопок без полей к корневому элементу вашего макета. Нет необходимости в атрибутах focusable или clickable, стиль по умолчанию инкапсулирует все это за вас.

<androidx.constraintlayout.widget.ConstraintLayout
    style="@android:style/Widget.Material.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

ПРИМЕЧАНИЕ. Добавьте стиль к ViewGroup в CardView, где находятся другие виды.

person Favour Felix Chinemerem    schedule 27.05.2020

Просто добавьте background в дочернее представление внутри MaterialCardView.

android:background="?attr/selectableItemBackground"

<com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:background="@color/white">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/item_view"
            android:layout_width="match_parent"
            android:background="?attr/selectableItemBackground"
            android:layout_height="wrap_content" >

    // -> rest of your design

  </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView
person Shihab Uddin    schedule 05.11.2020