如何删除 android 中 Recyclerview 项目之间的分隔符

how to remove divider between items of Recyclerview in android

我想删除 RecyclerView

项目之间的分隔符 (space)

所以尝试将项目视图的backgroundRecyclerView设置为White,但它不起作用 如何解决?

项目视图 XML :

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <LinearLayout
        android:background="@android:color/white"
        android:paddingLeft="@dimen/footer_item_padding"
        android:paddingRight="@dimen/footer_item_padding"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/img_avatar_category_item_adapter"
            android:contentDescription="@string/app_name"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:layout_width="@dimen/image_width_category_adapter"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

Activity XML :

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_categories_main_activity"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

Activity Class :

    rv_categories.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    rv_categories.setItemAnimator(new DefaultItemAnimator());

The problem is because of CardView.

在您的 CardView 上设置 setPreventCornerOverlap(false)。

在布局中添加下一行

<android.support.v7.widget.CardView
 xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

使用下面的标签

card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"

您可以使用 DividerItemDecoration class 并重写它的 onDraw 方法来不做任何事情:

mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, LinearLayoutManager.VERTICAL) {
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            // Do not draw the divider
        }
    });

First define your RecyclerView :

RecyclerView recycle =(RecyclerView) findViewById(R.id.recyclerView);

and in your activity use this method:

recycle.addItemDecoration(new DividerItemDecoration(context, 0));

不要在你的代码中使用下面的代码行,它解决了 iisue

groceryRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.HORIZONTAL));

recycle.addItemDecoration(新的 DividerItemDecoration(上下文,0));

添加

android:divider="@null"
android:dividerHeight="0dp"

到 recyclerView xml。

出于某种原因,其他答案对我不起作用,但此变通方法有效:

for (int i = 0; i < recyclerView.getItemDecorationCount(); i++) {
    if (recyclerView.getItemDecorationAt(i) instanceof DividerItemDecoration)
        recyclerView.removeItemDecorationAt(i);
}