这是之前在ListView类中使用divider和dividerHeight参数实现的一个例子:
<ListView
android:id="@+id/activity_home_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="8dp"/>
然而,在RecyclerView类中我没有看到这样的可能性。
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_home_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
在这种情况下,是否可以定义边距和/或直接添加自定义分隔符视图到列表项的布局中,或者是否有更好的方法来实现我的目标?
public class CommonItemSpaceDecoration extends RecyclerView.ItemDecoration {
private int mSpace = 0;
private boolean mVerticalOrientation = true;
public CommonItemSpaceDecoration(int space) {
this.mSpace = space;
}
public CommonItemSpaceDecoration(int space, boolean verticalOrientation) {
this.mSpace = space;
this.mVerticalOrientation = verticalOrientation;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.top = SizeUtils.dp2px(view.getContext(), mSpace);
if (mVerticalOrientation) {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.set(0, SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace));
} else {
outRect.set(0, 0, 0, SizeUtils.dp2px(view.getContext(), mSpace));
}
} else {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, 0, 0);
} else {
outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace), 0);
}
}
}
}
这将增加每个项目的顶部和底部(或左右)的空间。然后你可以将它设置为你的recyclerView。
recyclerView.addItemDecoration(new CommonItemSpaceDecoration(16));
文件SizeUtils.java
public class SizeUtils {
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
只需在回收器视图对象的init之后添加这一行。
在片段:
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),android.R.drawable.divider_horizontal_bright));
在活动
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,android.R.drawable.divider_horizontal_bright));
项目装饰
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
public static final int VERTICAL_LIST = 0;
public DividerItemDecoration(ListActivity listActivity, Object p1) {
}
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
/**
* Default divider will be used
*/
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
mDivider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
/**
* Custom divider will be used
*/
public DividerItemDecoration(Context context, int resId) {
mDivider = 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 + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
简单的方法是为RecyclerView设置背景色,为项目设置不同的背景色。这里有一个例子……
<android.support.v7.widget.RecyclerView
android:background="#ECEFF1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
和TextView项目(它可以是任何东西)与底线“x”dp或px。
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:background="#FFFFFF"/>
输出……
使用RecyclerView itemdecoration -一个基本的分隔符样本在Kotlin android
一个完整的示例,包括一个构建器,添加边缘或使用资源颜色的可能性
class SeparatorDecoration constructor(ctx: Context, @ColorRes dividerColor: Int, heightDp: Float) :
RecyclerView.ItemDecoration() {
private val mPaints = Paint()
init {
mPaints.color = dividerColor
val thickness = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
heightDp,
ctx.resources.displayMetrics
)
mPaints.strokeWidth = thickness
}
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val params = view.layoutParams as RecyclerView.LayoutParams
// and add a separator to any view but the last one
val p = params.viewAdapterPosition
// and add a separator to any view but the last one
if (p < state.itemCount) outRect[0, 0, 0] =
mPaints.strokeWidth.toInt() // left, top, right, bottom
else outRect.setEmpty() // 0, 0, 0, 0
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
// we set the stroke width before, so as to correctly draw the line we have to offset by width / 2
val offset = (mPaints.strokeWidth / 2).roundToInt()
// this will iterate over every visible view
for (i in 0 until parent.childCount) {
// get the view
val view: View = parent.getChildAt(i)
val params = view.layoutParams as RecyclerView.LayoutParams
// get the position
val position = params.viewAdapterPosition
// and finally draw the separator
if (position < state.itemCount) {
c.drawLine(
view.left.toFloat(),
(view.bottom + offset).toFloat(),
view.right.toFloat(), (view.bottom + offset).toFloat(), mPaints
)
}
}
}
}
设置很简单。只需将您的装饰与其余的初始设置一起添加到您的recyclerView。
val decoration = SeparatorDecoration(context, R.color.primaryColor, 1.5f)
recyclerview.addItemDecoration(decoration)
•PrimeAdapter
通过使用PrimeAdapter,在RecyclerViews中处理分隔符可以非常简单。它为创建和管理分隔器提供了多种功能和更大的灵活性。
您可以按以下方式创建适配器(请参阅GitHub中关于使用的完整文档):
val adapter = PrimeAdapter.with(recyclerView)
.setLayoutManager(LinearLayoutManager(activity))
.set()
.build(ActorAdapter::class.java)
创建适配器实例后,您可以简单地使用以下命令添加分隔符:
//----- Default divider:
adapter.setDivider()
//----- Divider with a custom drawable:
adapter.setDivider(ContextCompat.getDrawable(context, R.drawable.divider))
//----- Divider with a custom color:
adapter.setDivider(Color.RED)
//----- Divider with a custom color and a custom inset:
adapter.setDivider(Color.RED, insetLeft = 16, insetRight = 16)
//----- Deactivate dividers:
adapter.setDivider(null)