Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divider is not shown when the RecyclerView is on a Dialog

Tags:

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();
like image 767
Damn Vegetables Avatar asked Feb 28 '18 12:02

Damn Vegetables


1 Answers

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.)

like image 93
mhsmith Avatar answered Sep 18 '22 05:09

mhsmith