Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add item divider for RecyclerView in Kotlin

Tags:

android

kotlin

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

like image 981
Erald Developer Avatar asked Sep 11 '19 09:09

Erald Developer


People also ask

How do I add a divider to my recycler view?

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.

How do I add space between items in RecyclerView?

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.

What is DividerItemDecoration?

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.

How do I remove spaces between items in RecyclerView?

Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.


2 Answers

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)
like image 175
Mohan Sai Manthri Avatar answered Sep 17 '22 05:09

Mohan Sai Manthri


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));
like image 33
Ankit Avatar answered Sep 21 '22 05:09

Ankit