我正在开发一个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"
/>
所有这些答案都让我接近了答案,但它们都遗漏了一个关键细节。经过一番研究,我发现最简单的方法是将以下3个步骤结合起来:
使用支持库的DividerItemDecoration
用正确的颜色创建分隔线
在主题中设置这个分隔符为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>
编辑:更新跳过最后的分隔符:
在使用了一些之后,我意识到它在最后一项之后画了一个分隔符,这很烦人。所以我修改了第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);
}
}
}
);
您可以创建一个简单的可重复使用的分隔器。
创建分频器:
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);
}
}
}
希望能有所帮助。
在res/drawable文件夹中创建一个单独的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);