我正在开发一个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"
/>
KOTLIN -如果你正在寻找自定义的颜色分割线之间的回收视图项目,那么这是一个解决方案,这是为我工作:
步骤1:给你的回收视图一个默认的项目装饰。recyclerView.addItemDecoration (androidx.recyclerview.widget。DividerItemDecoration(活动,androidx.recyclerview.widget.LinearLayoutManager.VERTICAL))
第二步:
添加一个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>
第三步:在你的应用主题中添加这一行。
<item name="android:listDivider">@drawable/your_drawable</item>
您可以创建一个简单的可重复使用的分隔器。
创建分频器:
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++)
To
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);
}
}
}
希望能有所帮助。
好了,如果不需要改变你的分割线颜色,只需对分割线装饰应用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);