这是之前在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"/>
在这种情况下,是否可以定义边距和/或直接添加自定义分隔符视图到列表项的布局中,或者是否有更好的方法来实现我的目标?
请注意一下Alex Fu在GitHub上的这个文件:
链接
它是“直接从支持演示中提取”的diveritemdecoration .java示例文件。
在我的项目中导入这个文件后,我能够很好地获得分隔线,并将其作为项目装饰添加到回收器视图。
下面是我的onCreateView在我的片段中包含Recyclerview的样子:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recycler_view, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
return rootView;
}
我确信可以做额外的造型,但这只是一个起点。:)
使用这个类在你的RecyclerView中设置分隔符。
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // Item position
int column = position % spanCount; // Item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // Top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // Item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // Item top
}
}
}
}
如果有人想为spaceBetween, paddingLeft, paddingTop, paddingRight和paddingBottom设置不同的值。
class ItemPaddingDecoration(
private val spaceBetween: Int,
private val paddingLeft: Int = spaceBetween,
private val paddingTop: Int = spaceBetween,
private val paddingRight: Int = spaceBetween,
private val paddingBottom: Int = spaceBetween
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)
val orientation = when (val layoutManager = parent.layoutManager) {
is LinearLayoutManager -> {
layoutManager.orientation
}
is GridLayoutManager -> {
layoutManager.orientation
}
else -> {
RecyclerView.HORIZONTAL
}
}
if (orientation == RecyclerView.HORIZONTAL) {
when {
position == 0 -> {
outRect.set(paddingLeft, paddingTop, spaceBetween, paddingBottom)
}
position < parent.adapter!!.itemCount - 1 -> {
outRect.set(0, paddingTop, spaceBetween, paddingBottom)
}
else -> {
outRect.set(0, paddingTop, paddingRight, paddingBottom)
}
}
} else {
when {
position == 0 -> {
outRect.set(paddingLeft, paddingTop, paddingRight, paddingBottom)
}
position < parent.adapter!!.itemCount - 1 -> {
outRect.set(paddingLeft, 0, paddingRight, spaceBetween)
}
else -> {
outRect.set(paddingLeft, 0, paddingRight, paddingBottom)
}
}
}
}
}