这是一个示例的示例,该示例可以使用除法器dividerHeight 参数在ListView类中完成此操作:

<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"/>

在那种情况下,可以定义边距和/或将自定义分隔符 View 直接添加到列表项的布局中,还是有更好的方法实现我的目标?

最佳答案

2016年10月更新
Android支持库的25.0.0版本引入了 DividerItemDecoration 类:

用法:

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
    layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

上一个答案
一些答案要么使用自那以后就已弃用的方法,要么不提供完整的解决方案,因此我尝试做一个简短的最新总结。

ListView不同,RecyclerView类没有与分隔符相关的参数。相反,您需要扩展ItemDecoration的内部类 RecyclerView :
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.topoutRect.leftoutRect.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()方法。
用法
要使用新类,请在VerticalSpaceItemDecoration中添加DividerSpaceItemDecorationRecyclerView,例如在片段的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's library可以简化物品装饰过程。还没有尝试过。
features包括:
  • 库存物品装饰品的集合,包括:
  • 项目间距水平/垂直分隔线。
  • 列表项
  • 关于android - 如何在RecyclerView中的项目之间添加分隔线和空格?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24618829/

    10-10 08:49