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();
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With