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