View gets its drawable state (pressed, etc…) from its parent

前端 未结 1 1277
栀梦
栀梦 2021-01-05 11:33

I have a ListView where each list item is a custom layout based on a RelativeLayout, it is clickable and it has a custom selector. En plus, it has a sub-RelativeLayout which

相关标签:
1条回答
  • 2021-01-05 12:08

    Can you please show your code of ListView's item and selectors? To my mind you've made some mistakes, when implemented it. Here is my example of item and its selectors. It works fine for me:

    Video, how my implementation works: >click to open<

    list_view_item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="40dp"
        android:background="@drawable/relative_selector">
    
        <RelativeLayout
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:background="@drawable/child_selector_1"
            android:clickable="true">
    
            <RelativeLayout
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/child_selector_2"
                android:clickable="true"/>
        </RelativeLayout>
    </RelativeLayout>
    

    relative_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <inset xmlns:android="http://schemas.android.com/apk/res/android">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_blue_bright" />
                </shape>
            </inset>
        </item>
    </selector>
    

    child_selector_1.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- pressed state -->
        <item android:state_pressed="true">
            <inset xmlns:android="http://schemas.android.com/apk/res/android">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_orange_dark" />
                </shape>
            </inset>
        </item>
    
        <!-- focused state -->
        <item android:state_focused="true">
            <inset xmlns:android="http://schemas.android.com/apk/res/android">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_orange_dark" />
                </shape>
            </inset>
        </item>
    
        <!-- normal state -->
        <item>
            <inset xmlns:android="http://schemas.android.com/apk/res/android">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_red_dark" />
                </shape>
            </inset>
        </item>
    </selector>
    
    0 讨论(0)
提交回复
热议问题