我正在开发一个android应用程序,我正在使用RecyclerView。我需要在RecyclerView中添加一个分隔符。
我试着加上-
recyclerView.addItemDecoration(new
DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
下面是我的XML代码-
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
/>
Bhuvanesh BS解决方案有效。Kotlin版本:
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.recyclerview.widget.RecyclerView
class DividerItemDecorator(private val mDivider: Drawable?) : RecyclerView.ItemDecoration() {
override fun onDraw(
canvas: Canvas,
parent: RecyclerView,
state: RecyclerView.State
) {
val dividerLeft = parent.paddingLeft
val dividerRight = parent.width - parent.paddingRight
for (i in 0 until parent.childCount - 1) {
val child = parent.getChildAt(i)
val dividerTop =
child.bottom + (child.layoutParams as RecyclerView.LayoutParams).bottomMargin
val dividerBottom = dividerTop + mDivider!!.intrinsicHeight
mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
mDivider.draw(canvas)
}
}
}