how to remove divider between items of Recyclerview in android
Asked Answered
R

5

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

enter image description here

Recrimination answered 4/12, 2015 at 12:33 Comment(1)
remove android:paddingLeft="@dimen/footer_item_padding" android:paddingRight="@dimen/footer_item_padding"Cowled
B
24

First define your RecyclerView :

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

and in your activity use this method:

recycle.addItemDecoration(new DividerItemDecoration(context, 0));
Bendy answered 5/11, 2017 at 9:16 Comment(3)
can we not remove it in XML directly ?Bournemouth
this will set the divider horizontally but doesn't remove completely the dividerBidet
Its meaningful to use LinearLayout.SHOW_DIVIDER_NONE rather than 0 hereAztec
T
9

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
        }
    });
Thralldom answered 10/3, 2017 at 11:48 Comment(3)
Finally, I found a solution :DRattan
What if we want to draw for certain rows but not others?Fadil
@Fadil I suggest you use method getChildAt() on RecyclerView to get visible View. Then call getChildAdapterPosition() with the View you got from the previous step as a parameter. Then judging by the position value you get, you apply your logic for drawing for this particular view. What to pass to getChildAt()? I suppose creating a while loop from 0 until that method return null will do the trick.Thralldom
H
6

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);
}
Haplography answered 1/11, 2020 at 21:25 Comment(1)
Removes horizontal and vertical separators. Thank you.Indict
B
1

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

Bombastic answered 27/3, 2018 at 13:41 Comment(2)
Only removing the code will work. Setting to 0 will not remove the devider. I tested.Oestrogen
This answers the question. It should be ticked.Polaris
F
-2

Add

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

to recyclerView xml.

Formidable answered 4/3, 2020 at 16:12 Comment(1)
No such propertyTree

© 2022 - 2024 — McMap. All rights reserved.