如何在Android RecyclerView中添加分隔线?

我正在开发一个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"
/>
242786 次浏览

所以这可能不是正确的方式,但我只是添加了一个视图到RecyclerView的单项视图(因为我不认为有一个内置的函数),像这样:

<View
android:layout_width="fill_parent"
android:layout_height="@dimen/activity_divider_line_margin"
android:layout_alignParentBottom="true"
android:background="@color/tasklist_menu_dividerline_grey" />

这意味着每一项都有一行在底部填充。我用#111111背景使它大约1dp高。这也给了它一种“3D”效果。

yqritc的RecyclerView-FlexibleDivider使它成为一行。首先将这个添加到你的build.gradle:

compile 'com.yqritc:recyclerview-flexibledivider:1.4.0' // requires jcenter()

现在你可以在你设置recyclerView的适配器的地方配置和添加一个分配器:

recyclerView.setAdapter(myAdapter);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(Color.RED).sizeResId(R.dimen.divider).marginResId(R.dimen.leftmargin, R.dimen.rightmargin).build());

正确的方法是为RecyclerView定义ItemDecoration,如下所示

SimpleDividerItemDecoration.java

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
 

public SimpleDividerItemDecoration(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}
 

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
 

int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
 

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
 

int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
 

mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}

或如果您正在使用kotlin: SimpleDividerItemDecoration.kt < / >强

class SimpleDividerItemDecoration(context: Context, @DrawableRes dividerRes: Int) : ItemDecoration() {


private val mDivider: Drawable = ContextCompat.getDrawable(context, dividerRes)!!


override fun onDrawOver(c: Canvas, parent: RecyclerView) {
val left = parent.paddingLeft
val right = parent.width - parent.paddingRight
val childCount = parent.childCount
for (i in 0 until childCount) {
val child: View = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top: Int = child.bottom + params.bottomMargin
val bottom = top + mDivider.intrinsicHeight
mDivider.setBounds(left, top, right, bottom)
mDivider.draw(c)
}
}
}
    

line_divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
 

<size
android:width="1dp"
android:height="1dp" />
 

<solid android:color="@color/dark_gray" />
 

</shape>

最后像这样设置

recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));

编辑

正如@Alan Solitar指出的那样

context.getResources().getDrawable(R.drawable.line_divider);

是折旧而不是你可以使用的

ContextCompat.getDrawable(context,R.drawable.line_divider);

只需在项目适配器的末尾添加一个视图:

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>

在2016年10月的更新中,支持库v25.0.0现在有了基本水平和垂直分隔符的默认实现!

https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html

 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));

为了让nj的回答更简单一点,你可以这样做:

public class DividerColorItemDecoration extends DividerItemDecoration {


public DividerColorItemDecoration(Context context, int orientation) {
super(context, orientation);
setDrawable(ContextCompat.getDrawable(context, R.drawable.line_divider));
}
}

所有这些答案都让我接近了答案,但它们都遗漏了一个关键细节。经过一番研究,我发现最简单的方法是将以下3个步骤结合起来:

  1. 使用支持库的DividerItemDecoration
  2. 用正确的颜色创建分隔线
  3. 在你的主题中设置这个分隔符listDivider

步骤1:在配置RecyclerView时

recyclerView.addItemDecoration(
new DividerItemDecoration(context, layoutManager.getOrientation()));

第二步:在res/drawable/divider_gray.xml这样的文件中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="1px" android:height="1px" />
<solid android:color="@color/gray" />
</shape>

步骤3:在应用程序的主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Other theme items above -->
<item name="android:listDivider">@drawable/divider_gray</item>
</style>

< p >编辑:更新到跳过最后一个分隔符: < br > 在使用了一些之后,我意识到它在最后一项之后画了一个分隔符,这很烦人。所以我修改了步骤1,如下所示,以覆盖在DividerItemDecoration中的默认行为(当然,创建一个单独的类是另一个选项)
recyclerView.addItemDecoration(
new DividerItemDecoration(context, layoutManager.getOrientation()) {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
// hide the divider for the last child
if (position == parent.getAdapter().getItemCount() - 1) {
outRect.setEmpty();
} else {
super.getItemOffsets(outRect, view, parent, state);
}
}
}
);

如果你想同时使用水平和垂直分隔符:

  1. 定义水平&垂直分隔图纸:

    horizontal_divider.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:height="1dip" />
    <solid android:color="#22000000" />
    </shape>
    

    vertical_divider.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="1dip" />
    <solid android:color="#22000000" />
    </shape>
    
  2. Add this code segment below:

    DividerItemDecoration verticalDecoration = new DividerItemDecoration(recyclerview.getContext(),
    DividerItemDecoration.HORIZONTAL);
    Drawable verticalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.vertical_divider);
    verticalDecoration.setDrawable(verticalDivider);
    recyclerview.addItemDecoration(verticalDecoration);
    
    
    DividerItemDecoration horizontalDecoration = new DividerItemDecoration(recyclerview.getContext(),
    DividerItemDecoration.VERTICAL);
    Drawable horizontalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.horizontal_divider);
    horizontalDecoration.setDrawable(horizontalDivider);
    recyclerview.addItemDecoration(horizontalDecoration);
    

不幸的是,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的空间(它是深灰色,将作为分隔符出现)。

下面是一个简单的自定义分割线(垂直分割线/ 1dp高度/黑色)的代码:

假设你有支持库:

compile "com.android.support:recyclerview-v7:25.1.1"

java代码

    DividerItemDecoration divider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.my_custom_divider));
recyclerView.addItemDecoration(divider);

然后custom_divide .xml文件示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="@android:color/black" />
</shape>

res /可拉的文件夹中创建一个单独的xml文件

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="@android:color/black" />
</shape>

将xml文件(your_file)连接到主要活动,如下所示:

DividerItemDecoration divider = new DividerItemDecoration(
recyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.your_file));
recyclerView.addItemDecoration(divider);

我认为你正在使用Fragments来拥有RecyclerView

在创建RecyclerViewLayoutManager对象后简单地添加这些行

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);

这就是它!

它支持水平和垂直方向。

你需要添加下一行…

mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

试试这个简单的单行代码

recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));

只需在RecycleView Adapter中的一个项目的底部添加一个x量的边距。

onCreateViewHolder

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);


layoutParams.setMargins(0, 0, 0, 5);
itemView.setLayoutParams(layoutParams);

您可以创建一个简单的可重复使用的分隔器。

创建分频器:

public class DividerItemDecorator extends RecyclerView.ItemDecoration {
private Drawable mDivider;


public DividerItemDecorator(Drawable divider) {
mDivider = divider;
}


@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();


int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);


RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();


int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();


mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
mDivider.draw(canvas);
}
}
}

创建分隔线:separator .xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/grey_300" />
</shape>

为你的Recyclerview添加分隔符:

RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(context, R.drawable.divider));
recyclerView.addItemDecoration(dividerItemDecoration);

删除最后一项的分隔符:

为了防止最后一项画分隔线,你必须改变这条线。

for (int i = 0; i < childCount; i++)

for (int i = 0; i < childCount-1; i++)

你的最终实现应该是这样的:

public class DividerItemDecorator extends RecyclerView.ItemDecoration {
private Drawable mDivider;


public DividerItemDecorator(Drawable divider) {
mDivider = divider;
}


@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();


int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++) {
View child = parent.getChildAt(i);


RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();


int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();


mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
mDivider.draw(canvas);
}
}
}

希望能有所帮助。

recyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));

对于垂直线使用LinearLayoutManager。垂直

芬兰湾的科特林版:

recyclerview.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))

我处理分隔器视图和分隔器Insets的方式是通过添加一个RecyclerView扩展。

1.

通过命名View或RecyclerView添加一个新的扩展文件:

RecyclerViewExtension.kt

并在RecyclerViewExtension中添加setDivider扩展方法。kt文件。

/*
* RecyclerViewExtension.kt
* */
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView




fun RecyclerView.setDivider(@DrawableRes drawableRes: Int) {
val divider = DividerItemDecoration(
this.context,
DividerItemDecoration.VERTICAL
)
val drawable = ContextCompat.getDrawable(
this.context,
drawableRes
)
drawable?.let {
divider.setDrawable(it)
addItemDecoration(divider)
}
}

2.

drawable包中创建一个Drawable资源文件,如recycler_view_divider.xml:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="10dp"
android:insetRight="10dp">


<shape>
<size android:height="0.5dp" />
<solid android:color="@android:color/darker_gray" />
</shape>


</inset>

你可以在android:insetLeftandroid:insetRight上指定左右保证金

3.

在你的活动或片段中,RecyclerView被初始化,你可以通过调用:

recyclerView.setDivider(R.drawable.recycler_view_divider)

4.

欢呼声🍺

RecyclerView row with divider. < / >

  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

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

我认为这是最简单的方法

mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);
// or DividerItemDecoration.HORIZONTALL
mDividerItemDecoration.setDrawable(getDrawable(R.drawable.myshape));
recyclerView.addItemDecoration(mDividerItemDecoration);

注意: myshape可以是你想要做分割线的高度的矩形

好了,如果不需要改变你的分割线颜色,只需对分割线装饰应用alpha。

GridLayoutManager透明的示例:

DividerItemDecoration horizontalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.HORIZONTAL);
horizontalDividerItemDecoration.getDrawable().setAlpha(50);
DividerItemDecoration verticalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.VERTICAL);
verticalDividerItemDecoration.getDrawable().setAlpha(50);
my_recycler.addItemDecoration(horizontalDividerItemDecoration);
my_recycler.addItemDecoration(verticalDividerItemDecoration);
你仍然可以通过设置颜色来改变分割线的颜色

GridLayoutManager设置tint的例子:

DividerItemDecoration horizontalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.HORIZONTAL);
horizontalDividerItemDecoration.getDrawable().setTint(getResources().getColor(R.color.colorAccent));
DividerItemDecoration verticalDividerItemDecoration = new DividerItemDecoration(WishListActivity.this,
DividerItemDecoration.VERTICAL);
verticalDividerItemDecoration.getDrawable().setAlpha(50);
my_recycler.addItemDecoration(horizontalDividerItemDecoration);
my_recycler.addItemDecoration(verticalDividerItemDecoration);

此外,您还可以尝试设置颜色滤镜,

 horizontalDividerItemDecoration.getDrawable().setColorFilter(colorFilter);

芬兰湾的科特林 -如果你正在寻找自定义的颜色分割线之间的回收者视图项目,那么这是一个解决方案,这是为我工作:

步骤1:给你的回收视图一个默认的项目装饰。recyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(activity, androidx.recyclerview.widget.LinearLayoutManager.VERTICAL))

< p > 步骤2: 添加一个xml绘图,指定所需颜色的大小-宽度和高度
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/your_color" />
</shape>

步骤3:在你的应用主题中添加这一行。

<item name="android:listDivider">@drawable/your_drawable</item>

来自Material-IO库的MaterialDividerItemDecoration是许多用例的完整解决方案,如列表项分割或节分割。它拥有开发人员可能需要的一切,如插图和样式,而无需实现自己的项目装饰。这个材料分频器指南+实现真的很好检查。