How to add item divider for RecyclerView in Kotlin

徘徊边缘 提交于 2020-12-05 11:12:33

问题


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


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/57886100/how-to-add-item-divider-for-recyclerview-in-kotlin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!