Efekt Ripple na Android Lollipop CardView

Próbuję uzyskać Widok CardView, aby wyświetlić efekt ripple po dotknięciu przez ustawienie atrybutu android: backgound w pliku XML activity zgodnie z opisem Tutaj {[3] } na stronie deweloperów Androida, ale to nie działa. Nie ma animacji, ale metoda w onClick jest wywoływana. Próbowałem też stworzyć falę.plik xml zgodnie z sugestią tutaj , ale ten sam wynik.

CardView, jak pojawia się w pliku XML aktywności:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="155dp"
    android:layout_height="230dp"
    android:elevation="4dp"
    android:translationZ="5dp"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:onClick="showNotices"
    android:background="?android:attr/selectableItemBackground"
    android:id="@+id/notices_card"
    card_view:cardCornerRadius="2dp">

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

Jestem stosunkowo nowy w Androidzie rozwój, więc mogłem popełnić kilka oczywistych błędów.
Z góry dzięki.

Author: Community, 2014-11-15

12 answers

Należy dodać następujący tekst do CardView:

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
 400
Author: Jerry Gu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-21 20:10:20

Udało mi się uzyskać efekt ripple na cardview przez:

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:clickable="true" 
    android:foreground="@drawable/custom_bg"/>

I dla custom_bg, które możesz zobaczyć w powyższym kodzie, musisz zdefiniować plik xml zarówno dla urządzeń lollipop(w pakiecie drawable-v21), jak i pre-lollipop(w pakiecie drawable). dla custom_bg w pakiecie drawable-v21 kod jest następujący:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
<item
    android:id="@android:id/mask"
    android:drawable="@android:color/white"/>
</ripple>

Dla custom_bg w pakiecie drawable, kod to:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/colorHighlight"></solid>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/navigation_drawer_background"></solid>
    </shape>
</item>
</selector>

Więc na urządzeniach pre-lollipop będziesz miał solidny efekt kliknięcia, a na urządzeniach lollipop będziesz miał ripple wpływ na widok karty.

 23
Author: Rahul Ahuja,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-25 19:42:03

Dodaj te dwa kody liniowe do widoku xml, aby uzyskać efekt ripple na widoku cardView.

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
 21
Author: Abdul Rizwan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-22 12:59:24

Efekt tętnienia został pominięty w bibliotece appcompat, która jest tym, czego używasz. Jeśli chcesz zobaczyć tętnienie, użyj wersji Android L i przetestuj ją na urządzeniu z Androidem L. Na stronie AppCompat v7:

" dlaczego nie ma zmarszczek na pre-lizaku? Wiele z tego, co pozwala RippleDrawable działać płynnie, to nowy Renderthread Androida 5.0. Aby zoptymalizować wydajność w poprzednich wersjach Androida, na razie zrezygnowaliśmy z funkcji RippleDrawable."

Sprawdź ten link tutaj Po Więcej informacji

 14
Author: Developer Paul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-15 04:27:44

Jeśli aplikacja minSdkVersion, nad którą pracujesz, ma poziom 9, możesz użyć:

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

Zamiast tego, zaczynając od poziomu 11, używasz:

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

Z dokumentacji:

Clickable - określa, czy ten widok reaguje na zdarzenia kliknięcia. Musi być wartością logiczną, "true" lub "false".

Foreground - definiuje drawable do rysowania nad treścią. Może być stosowany jako nakładka. Pierwszoplanowe rysowanie bierze udział w wypełnianiu treści jeśli grawitacja jest ustawiona na wypełnienie.

 8
Author: Filipe de Lima Brito,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 12:56:34

Jeśli istnieje układ główny, taki jak RelativeLayout lub LinearLayout, który zawiera cały komponent adaptera w CardView, musisz ustawić atrybut tła w tym układzie głównym. like:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="122dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
card_view:cardCornerRadius="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/touch_bg"/>
</android.support.v7.widget.CardView>
 2
Author: Robert Lee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-21 08:48:57

Dla mnie dodanie foreground do CardView nie zadziałało (powód nieznany:/)

Dodanie tego samego do układu potomnego załatwiło sprawę.

Kod:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:focusable="true"
    android:clickable="true"
    card_view:cardCornerRadius="@dimen/card_corner_radius"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:id="@+id/card_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:padding="@dimen/card_padding">

    </LinearLayout>
</android.support.v7.widget.CardView>
 2
Author: Prabs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-11 10:25:54

Dodaj te dwa jak kod działa jak urok dla każdego widoku jak przycisk, układ liniowy lub CardView po prostu umieść te dwie linie i zobacz magię...

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
 1
Author: Hitesh Kushwah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-22 13:21:00

Nie byłem zadowolony z AppCompat, więc napisałem własny CardView i backported ripples. Tutaj działa na Galaxy S z Gingerbread, więc jest to zdecydowanie możliwe.

Demo Ripple na Galaxy S

Aby uzyskać więcej informacji sprawdź kod źródłowy .

 1
Author: Zielony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 10:42:25

Ripple event dla Androida Cardview Kontrola:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp" />
 1
Author: chavantr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 10:43:15

Dla tych, którzy szukają rozwiązania problemu efektu ripple, który nie działa na programowo utworzonym CardView (lub w moim przypadku custom view, który rozszerza CardView), który jest wyświetlany w RecyclerView, poniższe zadziałało dla mnie. Zasadniczo deklarowanie atrybutów XML wymienionych w innych odpowiedziach deklaratywnie w pliku układu XML nie wydaje się działać dla programowo utworzonego CardView, lub jeden utworzony z układu niestandardowego (nawet jeśli główny widok jest CardView lub scalać element jest używane), więc muszą być ustawione programowo tak:

private class MadeUpCardViewHolder extends RecyclerView.ViewHolder {
    private MadeUpCardView cardView;

    public MadeUpCardViewHolder(View v){
        super(v);

        this.cardView = (MadeUpCardView)v;

        // Declaring in XML Layout doesn't seem to work in RecyclerViews
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int[] attrs = new int[]{R.attr.selectableItemBackground};
            TypedArray typedArray = context.obtainStyledAttributes(attrs);
            int selectableItemBackground = typedArray.getResourceId(0, 0);
            typedArray.recycle();

            this.cardView.setForeground(context.getDrawable(selectableItemBackground));
            this.cardView.setClickable(true);
        }
    }
}

Gdzie MadeupCardView extends CardView Pozdrawiam ta ODPOWIEDŹ dla TypedArray części.

 0
Author: Breeno,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-05 16:14:13
  android:foreground="?android:attr/selectableItemBackgroundBorderless"
   android:clickable="true"
   android:focusable="true"

Działa tylko api 21 i użyj tego nie używaj widoku karty wiersza listy

 0
Author: Tarun Umath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-18 13:54:38