我正在开发一个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"
/>
class ItemOffsetDecoration(
context: Context,
private val paddingLeft: Int,
private val paddingRight: Int
) : RecyclerView.ItemDecoration() {
private var mDivider: Drawable? = null
init {
mDivider = ContextCompat.getDrawable(context, R.drawable.divider_medium)
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val left = parent.paddingLeft + paddingLeft
val right = parent.width - parent.paddingRight - paddingRight
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top = child.bottom + params.bottomMargin
val bottom = top + (mDivider?.intrinsicHeight ?: 0)
mDivider?.let {
it.setBounds(left, top, right, bottom)
it.draw(c)
}
}
}
}
你只需要在R.drawable.divider_medium中指定一个颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/black" />
<size
android:height="1dp"
android:width="1dp" />
</shape>
并将它添加到您的recyclerView
recyclerView.addItemDecoration(
ItemOffsetDecoration(
this,
resources.getDimension(resources.getDimension(R.dimen.dp_70).roundToInt()).roundToInt(),
0
)
)
refernce这
不幸的是,Android只是把小事情变得太复杂了。实现你想要的最简单的方法,没有实现这里的DividerItemDecoration:
为RecyclerView添加你想要的分割线颜色的背景色:
<RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorLightGray"
android:scrollbars="vertical"
tools:listitem="@layout/list_item"
android:background="@android:color/darker_gray"/>
添加底部边距(android:layout_marginBottom)到项目的布局根(list_item.xml):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John Doe" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:text="Some description blah blah" />
</RelativeLayout>
这将在项目和RecyclerView的背景色之间提供1dp的空间(它是深灰色,将作为分隔符出现)。
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)
}
}
}