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

当前回答

我认为这是最简单的方法

mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                DividerItemDecoration.VERTICAL);
// or DividerItemDecoration.HORIZONTALL
        mDividerItemDecoration.setDrawable(getDrawable(R.drawable.myshape));
        recyclerView.addItemDecoration(mDividerItemDecoration);

注意:myshape可以是你想要做分隔的高度的矩形

其他回答

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

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

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

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

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

就是这样!

它支持水平和垂直方向。

我认为这是最简单的方法

mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                DividerItemDecoration.VERTICAL);
// or DividerItemDecoration.HORIZONTALL
        mDividerItemDecoration.setDrawable(getDrawable(R.drawable.myshape));
        recyclerView.addItemDecoration(mDividerItemDecoration);

注意:myshape可以是你想要做分隔的高度的矩形

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

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

在res/drawable文件夹中创建一个单独的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>

在主活动中连接那个xml文件(your_file),像这样:

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