0

I have a simple RecyclerView in which each row displays a line of text. Each row is selectable and so I want to use

android:background="?attr/selectableItemBackground"

The problem is that RecyclerViews do not allow for dividers; whereas I want dividers. Hence I need a background as below (called bottom_line.xml)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#FF000000" />

            <solid android:color="#00FFFFFF" />

            <padding android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</layer-list>

My question is how do I combine my bottom_line.xml with ?attr/selectableItemBackground to create a final drawable with ripples? (I am trying to avoid using a TextView inside a LinearLayout)

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

2 Answers2

0

You can add dividers in several ways.

One way is that your cell layout can have a divider in it, which is just a View with 1dp height and gray background. So you don't have to worry about combining background drawables.

CQM
  • 42,592
  • 75
  • 224
  • 366
0

RecyclerView does indeed allow for dividers.

Add a DividerItemDecoration class to implement the drawing, e.g. https://gist.github.com/alexfu/0f464fc3742f134ccd1e

Use addItemDecoration on your RecyclerView.

myRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST, R.drawable.divider));

Set your divider drawable, this example has left and right insets and a gradient

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetRight="16dp">

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient android:startColor="@color/dividerEdge" android:centerColor="@color/dividerCenter" android:endColor="@color/dividerEdge" android:angle="0" />
    </shape>

</inset>

Alternatively, you can add a ripple effect (API 21+) to your XML by adding:

<ripple android:color="@color/somecolor">
  <item> ... </item>
</ripple>

though it seems that you might not need this...

CSmith
  • 13,318
  • 3
  • 39
  • 42