how to remove divider between items of Recyclerview in android

馋奶兔 提交于 2021-02-04 13:04:17

问题


i want to remove divider (space) between items of RecyclerView

So try to set background of item view and RecyclerView to White,but it doesn't works how to fix it ?

Item View 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());


回答1:


First define your RecyclerView :

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

and in your activity use this method:

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



回答2:


You can use DividerItemDecoration class and override its onDraw method to do nothing like so:

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



回答3:


Dont use below line of code in your code, its solve the iisue

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

or

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




回答4:


Add

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

to recyclerView xml.




回答5:


For some reason the other answers didn't work for me but this workaround did:

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



回答6:


The problem is because of CardView.

Set setPreventCornerOverlap(false) on your CardView.

OR

Add below line in layout

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

Use below tag

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



回答7:


Among several related properties can also be the elevation



来源:https://stackoverflow.com/questions/34088536/how-to-remove-divider-between-items-of-recyclerview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!