I'm working on an app where I have a list with recyclerview and I want to add a divider for items. I have created the ItemDividerDecorator class and xml layout file but I'm not connecting to recycler view.
I know how to do in java, something like this:
recyclerView.addItemDecoration(
new DividerItemDecoration(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.item_separator)));
but how can I do in Kotlin, I also tried to convert it in Android Studio but shows me a couples of errors. Here is my Decorator class:
private val mdivider:Drawable
init{
this.mdivider = mdivider
}
override fun onDrawOver(canvas: Canvas, parent:RecyclerView, state:RecyclerView.State) {
val left = parent.getPaddingLeft()
val right = parent.getWidth() - parent.getPaddingRight()
val childCount = parent.getChildCount()
for (i in 0 until childCount)
{
val child = parent.getChildAt(i)
val params = child.getLayoutParams() as RecyclerView.LayoutParams
val top = child.getBottom() + params.bottomMargin
val bottom = top + mdivider.getIntrinsicHeight()
mdivider.setBounds(left, top, right, bottom)
mdivider.draw(canvas)
}
}
Any help is appreciated
Unlike ListView , the RecyclerView class doesn't have any divider-related parameters. Instead, you need to extend ItemDecoration , a RecyclerView 's inner class: An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set.
The best way is to add the ItemDecoration to the RecyclerView as it affects the individual items and you can have more control over them. The above class extends ItemDecoration and overrides getItemOffsets where the left, right, bottom margins are set based on the constructor injected margins.
DividerItemDecoration is a RecyclerView. ItemDecoration that can be used as a divider between items of a LinearLayoutManager . It supports both HORIZONTAL and VERTICAL orientations. mDividerItemDecoration = new DividerItemDecoration(recyclerView.
Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.
For Kotlin:
recycler_view.addItemDecoration(
DividerItemDecoration(
context,
LinearLayoutManager.HORIZONTAL
)
)
If you intialized like this:
private lateint var context:Context
then inside your onCreateView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Intialize context here
context = parent.context()
rootView = container?.inflateView(layoutToInflate) ?: View(context)
onFragmentCreated(rootView)
return rootView
}
If you're using inside an activity then instead use
applicationContext
val decorator = DividerItemDecoration(applicationContext, LinearLayoutManager.VERTICAL)
decorator.setDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.file)!!)
recycler_view.addItemDecoration(decorator)
Try this for kotlin
for default item separator
recyclerview.addItemDecoration(DividerItemDecoration(this@YourActivity, LinearLayoutManager.VERTICAL))
drawable as a item separator
val divider = DividerItemDecoration(this@MainActivity,DividerItemDecoration.VERTICAL)
divider.setDrawable(ContextCompat.getDrawable(this@MainActivity,R.drawable.item_separator)!!)
recyclerview.addItemDecoration(divider)
for java
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
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