简体   繁体   中英

Strange onClick highlighting for CardView

I'm running into some strange behavior for a CardView that I have set as clickable. Clicking it highlights all the views inside the card slightly differently, which makes it look weird when you stop pressing on the card.

Here is what it looks like when the card is clicked . Ideally I'd like the entire card to turn pink when your finger is on it. When you let go, the card evenly fades back to white.

Any advice is appreciated!

card_class.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cv"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    android:clickable="true"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:background="@drawable/button_rect_list_normal">
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:clickable="false"
                android:background="@drawable/button_rect_list_normal">

                <TextView
                    android:id="@+id/class_name"
                    android:gravity="center_vertical"
                    android:paddingStart="16dp"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingTop="18dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="24sp"
                    android:textColor="@color/text"
                    android:clickable="false"
                    android:text="@string/classname_placeholder"
                    android:background="@drawable/button_rect_list_normal"
                    android:paddingBottom="18dp"
                    android:autoText="true" />

                <TextView
                    android:id="@+id/class_teacher"
                    android:gravity="center_vertical"
                    android:paddingStart="16dp"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingBottom="24dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    android:textColor="@color/secondary2"
                    android:clickable="false"
                    android:text="@string/teacher_placeholder"
                    android:background="@drawable/button_rect_list_normal" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/period_placeholder"
                android:id="@+id/class_period"
                android:elegantTextHeight="false"
                android:singleLine="true"
                android:paddingRight="16dp"
                android:textSize="57sp"
                android:gravity="center_vertical" />
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

button_rect_list_normal.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <item android:drawable="@drawable/selected" android:state_selected="true"/>
    <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

pressed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F44336"/>
</shape>

selected.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFCDD2"/>
</shape>

You should be applying your drawable to the root CardView if you want the background to change for the whole card - I'm not sure why you have different background drawables applied to multiple ViewGroup 's.

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cv"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    android:clickable="true"
    android:foreground="@drawable/button_rect_list_normal"
    android:layout_height="wrap_content">

EDIT: Changed android:background to android:foreground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM