2

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.

Community
  • 1
  • 1
Dre
  • 543
  • 1
  • 4
  • 17
  • What happens if you use `wrap_content ` instead? – MeetTitan May 08 '15 at 09:23
  • That didn't work. It did give me the idea to also set the height of the 'pressed' drawable to a large number but that didn't help either. – Dre May 08 '15 at 09:26
  • What happens if you change the height of the `RelativeLayout` container to—say, 32dp—and change the selector view's height to `match parent`? – MeetTitan May 08 '15 at 09:28
  • That works with match_parent and wrap_content. I still need to have the item expand to content though. – Dre May 08 '15 at 09:31
  • If you want to draw a selector at the top. put the text view in a layout. set width and height of that layout match_parent. then add some padding . don't place view in that layout . – Muhammad Adil May 08 '15 at 09:43
  • @MuhammadAdil Sorry, I'm not sure what you mean. I want the selector drawn on top as in covering the text view in the same grid item. – Dre May 08 '15 at 09:47
  • If setting the `RelativeView` height to a static value, and setting the selector view to `match_parent` works, then (I assume) it is a problem with having `wrap_content` on the parent, and having `match_parent` on the child. I think this is a circular reference, since the parent looks to the child for sizing, and the child looks to the parent for sizing. I will look into this and get back to you. – MeetTitan May 08 '15 at 10:06
  • Also, which version of android are you on? I solved this in my project with the ripple effect, but this may not be applicable to every scenario. Could you change the background of the view on selection and change it back when cleared? – MeetTitan May 08 '15 at 10:08
  • Thanks. My minSdkVersion is 17. I'm testing on a Nexus 9 running 5.0.2. – Dre May 08 '15 at 10:11
  • OK. can you tell me which view can get expand. is it textView ?? – Muhammad Adil May 08 '15 at 10:36
  • Um, I'm not sure what mean by 'can get expand'. I'm trying to expand the selector view to match the whole grid item if that's what you mean? – Dre May 08 '15 at 10:38

1 Answers1

0

you can setLayoutParams for RecyclerView's child view

E.Sky
  • 1