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

Shishram
  • 1,514
  • 11
  • 20
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71

5 Answers5

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));
nima barati
  • 345
  • 3
  • 8
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
        }
    });
Sevastyan Savanyuk
  • 5,797
  • 4
  • 22
  • 34
  • Finally, I found a solution :D – Tefa Aug 19 '17 at 00:44
  • 2
    What if we want to draw for certain rows but not others? – lostintranslation Feb 23 '18 at 19:01
  • @lostintranslation 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. – Sevastyan Savanyuk Feb 24 '18 at 13:02
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);
}
einUsername
  • 1,569
  • 2
  • 15
  • 26
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));

Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
-1

Add

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

to recyclerView xml.

loic .B
  • 281
  • 2
  • 11