我正在开发一个android应用程序,我正在使用RecyclerView。我需要在RecyclerView中添加一个分隔符。 我试着加上-

recyclerView.addItemDecoration(new
     DividerItemDecoration(getActivity(),
       DividerItemDecoration.VERTICAL_LIST));

下面是我的XML代码-

   <android.support.v7.widget.RecyclerView
    android:id="@+id/drawerList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    />

当前回答

yqritc的RecyclerView-FlexibleDivider使它成为一个内衬。首先把这个添加到你的build.gradle:

compile 'com.yqritc:recyclerview-flexibledivider:1.4.0' // requires jcenter()

现在你可以在你设置recyclerView的适配器的地方配置和添加一个分配器:

recyclerView.setAdapter(myAdapter);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(Color.RED).sizeResId(R.dimen.divider).marginResId(R.dimen.leftmargin, R.dimen.rightmargin).build());

其他回答

您可以创建一个简单的可重复使用的分隔器。

创建分频器:

public class DividerItemDecorator extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public DividerItemDecorator(Drawable divider) {
        mDivider = divider;
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int dividerLeft = parent.getPaddingLeft();
        int dividerRight = 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 dividerTop = child.getBottom() + params.bottomMargin;
            int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();

            mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
            mDivider.draw(canvas);
        }
    }
}

创建分隔线:separator .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="1dp" />
    <solid android:color="@color/grey_300" />
</shape>

为你的Recyclerview添加分隔符:

RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(context, R.drawable.divider));
recyclerView.addItemDecoration(dividerItemDecoration);

删除最后一项的分隔符:

为了防止最后一项画分隔线,你必须改变这条线。

for (int i = 0; i < childCount; i++) 

To

for (int i = 0; i < childCount-1; i++)

你的最终实现应该是这样的:

public class DividerItemDecorator extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public DividerItemDecorator(Drawable divider) {
        mDivider = divider;
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int dividerLeft = parent.getPaddingLeft();
        int dividerRight = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int dividerTop = child.getBottom() + params.bottomMargin;
            int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();

            mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
            mDivider.draw(canvas);
        }
    }
}

希望能有所帮助。

如果你想同时使用水平和垂直分隔符:

Define horizontal & vertical divider drawables: horizontal_divider.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:height="1dip" /> <solid android:color="#22000000" /> </shape> vertical_divider.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="1dip" /> <solid android:color="#22000000" /> </shape> Add this code segment below: DividerItemDecoration verticalDecoration = new DividerItemDecoration(recyclerview.getContext(), DividerItemDecoration.HORIZONTAL); Drawable verticalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.vertical_divider); verticalDecoration.setDrawable(verticalDivider); recyclerview.addItemDecoration(verticalDecoration); DividerItemDecoration horizontalDecoration = new DividerItemDecoration(recyclerview.getContext(), DividerItemDecoration.VERTICAL); Drawable horizontalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.horizontal_divider); horizontalDecoration.setDrawable(horizontalDivider); recyclerview.addItemDecoration(horizontalDecoration);

下面是一个简单的自定义分割线(垂直分割线/ 1dp高度/黑色)的代码:

假设你有支持库:

compile "com.android.support:recyclerview-v7:25.1.1"

java代码

    DividerItemDecoration divider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.my_custom_divider));
    recyclerView.addItemDecoration(divider);

然后custom_divide .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="@android:color/black" />
</shape>

所以这可能不是正确的方式,但我只是添加了一个视图到RecyclerView的单项视图(因为我不认为有一个内置的函数),像这样:

<View
    android:layout_width="fill_parent"
    android:layout_height="@dimen/activity_divider_line_margin"
    android:layout_alignParentBottom="true"
    android:background="@color/tasklist_menu_dividerline_grey" />

这意味着每一项都有一行在底部填充。我把它做成1dp高,背景#111111。这也给了它一种“3D”效果。

只需在项目适配器的末尾添加一个视图:

<View
 android:layout_width="match_parent"
 android:layout_height="1dp"
 android:background="#FFFFFF"/>