View gets its available state (click, etc.) from its parent

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

Something like that:

--------------- | | | ___ | | | | | --------------- 

Everything works fine, but the problem is that when I click on the parent relative layout, the child selector assumes the state of the parent selector. I tried to install

 android:duplicateParentState="false" 

for a child, but nothing has changed.

Any idea? thanks in advance

+6
source share
1 answer

Can you show your ListView list item code and selectors? In my opinion, you made some mistakes when it is implemented. Here is my example of an element and its selectors. This 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> 
+2
source

Source: https://habr.com/ru/post/903073/


All Articles