Similar to CardView layout_width="match_parent" does not match parent RecyclerView width but I'm trying to get layout_height="match_parent"
working.
I am trying to reproduce the GridView
's drawSelectorOnTop
with a RecyclerView
.
My list item's simplified xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_category_adapter_item_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight">
<TextView
android:id="@+id/menu_category_adapter_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_color_main"/>
<View
android:id="@+id/menu_category_adapter_item_selector"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_list_item"
android:clickable="true"/>
</RelativeLayout>
The code inflating the layout is:
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_category_adapter_item, parent, false));
Selector drawable bg_list_item.xml is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/bg_list_item_pressed"/>
<item android:state_selected="true" android:drawable="@drawable/bg_list_item_selected"/>
<item android:drawable="@drawable/bg_list_item_normal"/>
</selector>
Drawable bg_list_item_pressed.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#3000"/>
</shape>
If I hard code the height e.g. layout_height="32dp"
for the selector view, the selector is shown however the items can change height so I can't hard code it as a workaround.
I have tried using layout_alignParentTop
and layout_alignParentBottom
with no luck.
Something strange to note is that it inconsistently works. On a grid of 8 items (2 rows of 4 items), items 1, 3 and 4 show the selector.