6

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();
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • 1
    `android.R.attr.listDivider` comes from the theme for the dialog. Is your dialog theme different than that of your fragment? Does it have `android.R.attr.listDivider` defined? Try adding `android.R.attr.listDivider` to your dialog theme to see if it works for you. – Cheticamp Feb 28 '18 at 14:58
  • I am not sure. I instantiated the dialogue programmatically (`val d = MyDialogue(context)`), and I did not specify any special theme for it. – Damn Vegetables Feb 28 '18 at 17:03
  • I think dialogs default to their own theme. I am fairly sure this is a theme issue. – Cheticamp Feb 28 '18 at 18:44
  • In that case, what is the cleanest and most recommended way to use divider other than the workaround code I put above? – Damn Vegetables Mar 01 '18 at 03:30

2 Answers2

9

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

mhsmith
  • 6,675
  • 3
  • 41
  • 58
-2

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