Damn Vegetables
Damn Vegetables

Reputation: 12504

Divider is not shown when the RecyclerView is on a Dialog

I used the exact same code below for both a RecyclerView in a Fragment and another RecyclerView in a Dialog.

    myAdapter = MyAdapter();
    var lm = LinearLayoutManager(this.context)
    myRecyclerView.layoutManager = lm;
    myRecyclerView.adapter = myAdapter;
    var line = DividerItemDecoration(this.context, lm.orientation);
    myRecyclerView.addItemDecoration(line);

The weird thing is, the divider line is shown in the Fragment, but NOT shown in the Dialog. Is this a known problem? Or did I do something wrong? I just wanted to show the in-built black line divider between items.

I called the code above in the constructor of my custom Dialog.

class MyDialogue:Dialog
{
    constructor(context: Context?) : super(context)
    {
        setContentView(R.layout.my_dialogue);
        window.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        //That code above.
    }

Added: It seemed the default line is using android.R.attr.listDivider. I just do not get why RecyclerView does not get it in a Dialog. As a workaround, I manually set that drawable to the decorator, and now I can see the default divider. The code is like below. But why should I have to do this?

val a = context!!.theme.obtainStyledAttributes(
               R.style.AppTheme, intArrayOf(android.R.attr.listDivider));
val attributeResourceId = a.getResourceId(0, 0)
val drawable = context.getDrawable(attributeResourceId)
line.setDrawable(drawable);
a.recycle();

Upvotes: 6

Views: 1664

Answers (2)

mhsmith
mhsmith

Reputation: 8121

listDivider is set to null in the default dialog theme, presumably because AlertDialog lists aren't supposed to have dividers. You can override this for a specific dialog by passing a different theme to DividerItemDecoration. So instead of:

DividerItemDecoration(this.context, lm.orientation)

Use this:

DividerItemDecoration(ContextThemeWrapper(this.context, R.style.AppTheme), lm.orientation)

Where AppTheme is your app's overall theme.

(Thanks to Cheticamp's comment for leading me in the right direction.)

Upvotes: 9

Jaimin Sarvan
Jaimin Sarvan

Reputation: 142

Use the Below Code

Create DividerItemDecoration.java as below:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
        mDivider = context.getResources().getDrawable(R.drawable.linedivider);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

Have a shape linedivider.xml in drawable as below:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="1dp"
        android:height="1dp" />

    <solid android:color="@color/colorline" />

</shape>

color in colors.xml:

<color name="colorline">#9f9f9f</color>

use the DividerItemDecoration.java as:

myRecyclerView.addItemDecoration(new DividerItemDecoration(this));

Upvotes: -2

Related Questions