2016年10月更新
Android支持库25.0.0版本引入了DividerItemDecoration类:
DividerItemDecoration是一个RecyclerView。ItemDecoration,可以用作LinearLayoutManager的项目之间的分隔符。它支持水平和垂直方向。
用法:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
以前的回答
有些答案使用的方法已经过时,或者没有给出完整的解决方案,所以我试着做一个简短的、最新的总结。
与ListView不同,RecyclerView类没有任何与分隔符相关的参数。相反,你需要扩展ItemDecoration,一个RecyclerView的内部类:
ItemDecoration允许应用程序向适配器数据集中的特定项视图添加特殊的绘图和布局偏移。这可以用于在项目之间绘制分隔线,高光,视觉分组边界等。
所有itemdecoration都按照它们被添加的顺序绘制,在项目视图之前(在onDraw())和在项目之后(在onDrawOver(Canvas, RecyclerView, RecyclerView. state)。
垂直间距
扩展ItemDecoration,添加一个自定义构造函数,将空格高度作为参数,并覆盖getitemoffset()方法:
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int verticalSpaceHeight;
public VerticalSpaceItemDecoration(int verticalSpaceHeight) {
this.verticalSpaceHeight = verticalSpaceHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = verticalSpaceHeight;
}
}
如果你不想在最后一项下面插入空格,添加以下条件:
if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
outRect.bottom = verticalSpaceHeight;
}
注意:你也可以修改outRect。前,outRect。left和outRect。正确的属性为期望的效果。
分配器ItemDecoration
扩展ItemDecoration并覆盖onDraw()方法:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable divider;
/**
* Default divider will be used
*/
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
divider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
/**
* Custom divider will be used
*/
public DividerItemDecoration(Context context, int resId) {
divider = ContextCompat.getDrawable(context, resId);
}
@Override
public void onDraw(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 + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
你可以调用第一个构造函数使用默认的Android divider属性,也可以调用第二个构造函数使用你自己的drawable,例如drawable/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="#ff992900" />
</shape>
注意:如果你想在你的项目上绘制分隔线,请重写onDrawOver()方法。
使用
要使用你的新类,添加VerticalSpaceItemDecoration或DividerSpaceItemDecoration到RecyclerView,例如在你的片段的onCreateView()方法中:
private static final int VERTICAL_ITEM_SPACE = 48;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
//add ItemDecoration
recyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
//or
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
//or
recyclerView.addItemDecoration(
new DividerItemDecoration(getActivity(), R.drawable.divider));
recyclerView.setAdapter(...);
return rootView;
}
还有Lucas Rocha的图书馆,旨在简化道具装饰过程。不过我还没试过。
其特点包括:
收藏的库存项目装饰,包括:
项目间距水平/垂直分隔。
列表项