Android 将分隔器添加到RecyclerView
示例
首先,您需要创建一个扩展类RecyclerView.ItemDecoration:
public class SimpleBlueDivider extendsRecyclerView.ItemDecoration{
private Drawable mDivider;
public SimpleBlueDivider(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.divider_blue);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent,RecyclerView.Statestate) {
//分隔符填充可根据需要或禁用任何填充
int left =parent.getPaddingLeft()+80;
int right = parent.getWidth() - parent.getPaddingRight()-30;
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParamsparams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}添加divider_blue.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="4dp" /> <solid android:color="#AA123456" /> </shape>
然后像这样使用它:
recyclerView.addItemDecoration(new SimpleBlueDivider(context));
结果将如下所示:
该图像只是分隔器如何工作的一个示例,如果要在添加分隔器时遵循材料设计规范,请查看以下链接:分隔器,并通过提供链接感谢@BrendenKromhout。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下