我正在开发一个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"
    />

当前回答

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

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>

我认为你是在使用Fragments来实现RecyclerView

在创建RecyclerView和LayoutManager对象后,只需添加这些行

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

就是这样!

它支持水平和垂直方向。

所有这些答案都让我接近了答案,但它们都遗漏了一个关键细节。经过一番研究,我发现最简单的方法是将以下3个步骤结合起来:

使用支持库的DividerItemDecoration 用正确的颜色创建分隔线 在主题中设置这个分隔符为listDivider


步骤1:在配置RecyclerView时

recyclerView.addItemDecoration(
        new DividerItemDecoration(context, layoutManager.getOrientation()));

第二步:在res/drawable/divider_gray.xml这样的文件中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="1px" android:height="1px" />
    <solid android:color="@color/gray" />
</shape>

步骤3:在应用程序的主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Other theme items above -->
    <item name="android:listDivider">@drawable/divider_gray</item>
</style>

编辑:更新跳过最后的分隔符: 在使用了一些之后,我意识到它在最后一项之后画了一个分隔符,这很烦人。所以我修改了第1步,如下所示,以覆盖在DividerItemDecoration的默认行为(当然,制作一个单独的类是另一种选择):

recyclerView.addItemDecoration(
        new DividerItemDecoration(context, layoutManager.getOrientation()) {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                // hide the divider for the last child
                if (position == parent.getAdapter().getItemCount() - 1) {
                    outRect.setEmpty();
                } else {
                    super.getItemOffsets(outRect, view, parent, state);
                }
            }
        }
);
recyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));

对于垂直线使用LinearLayoutManager。垂直

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

创建分频器:

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);
        }
    }
}

希望能有所帮助。