2016年10月更新
Android支持库的25.0.0版本引入了以下DividerItemDecoration
类:
DividerItemDecoration是RecyclerView.ItemDecoration,可以用作的项之间的分隔线LinearLayoutManager
。它同时支持HORIZONTAL
和VERTICAL
方向。
用法:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
先前的答案
一些答案要么使用自那以后就已弃用的方法,要么不提供完整的解决方案,因此我尝试进行简短的最新总结。
不同于ListView
,RecyclerView
该类没有与分隔符相关的参数。相反,您需要扩展ItemDecoration
一个RecyclerView
的内部类:
一个ItemDecoration
允许应用程序添加偏移量从适配器的数据集特定项目视图的特殊的绘图和布局。这对于在项目,突出显示,可视分组边界等之间绘制分隔线很有用。
所有ItemDecorations
被描绘在加入他们的顺序,(在项目视图之前onDraw()
)和项目后(在onDrawOver( ,Canvas
,。RecyclerView
RecyclerView.State)
Vertical
间距 ItemDecoration
扩展ItemDecoration
,添加以空格height
为参数并覆盖getItemOffsets()
方法的自定义构造函数:
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.top
,outRect.left
以及outRect.right
对预期效果的性质。
分频器 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除法器属性的构造函数,也可以调用第二个使用您自己的drawable的构造函数,例如drawable / divider.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()
改用method。
用法
要使用新类add VerticalSpaceItemDecoration
或DividerSpaceItemDecoration
to 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的图书馆,应该可以简化物品装饰过程。还没有尝试过。
其功能包括:
- 库存物品装饰的集合,包括:
- 项目间距水平/垂直分隔线。
- 项目清单