Remove spacing between items in RecyclerView android

32,943

Solution 1

just remove the cardview in your layout/cardview_recycler.xml, android puts that spacing that you don't want

<LinearLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <mypackagename.ui.view.RecyclingImageView
        android:id="@+id/avatar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/img_no_avatar" />

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="end"
        android:padding="@dimen/spacing"
        android:textColor="@color/black"
        android:layout_alignParentBottom="true"
        android:textSize="@dimen/text_smallest" />
</LinearLayout>

everything else stays as it is

Solution 2

Give android:layout_height = wrap_content instead of match_parent to your item root layout

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
       -------
       -------
</android.support.v7.widget.CardView>

Solution 3

In my case below code worked

<android.support.v7.widget.RecyclerView
            android:id="@+id/freshRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:scrollbars="vertical" />
Share:
32,943

Related videos on Youtube

fvasquezjatar
Author by

fvasquezjatar

Microsoft Certified Professional Developer | Android Developer

Updated on May 30, 2020

Comments

  • fvasquezjatar
    fvasquezjatar about 4 years

    I am using a RecyclerView to show a List of Items and I need to remove the default spacing between Items. I'm trying to set a RecyclerView.LayoutParams and set margins to zero on my LinearLayoutManager but didn't work!

    Here is my code:

    layout/fragment_recycler.xml

       <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/freshSwipeView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/freshRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </android.support.v4.widget.SwipeRefreshLayout>
    

    layout/cardview_recycler.xml

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
    
            <mypackagename.ui.view.RecyclingImageView
                android:id="@+id/avatar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:src="@drawable/img_no_avatar" />
    
            <TextView
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:ellipsize="end"
                android:padding="@dimen/spacing"
                android:textColor="@color/black"
                android:layout_alignParentBottom="true"
                android:textSize="@dimen/text_smallest" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
    

    RecyclerFragment.java

        /* Setting Layout Manager */
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        mLayoutManager.canScrollVertically();
        mRecyclerView.setLayoutManager(mLayoutManager);
    

    I need help...

    • Varvara Kalinina
      Varvara Kalinina over 8 years
      Why do you define but never use params in your code?
  • Zapnologica
    Zapnologica about 9 years
    Is there no way to adjust the spacing which the card view puts? I want to keep the spacing, but I don't want like 20dp space?
  • blackHawk
    blackHawk almost 5 years
    why cardview adds spacing?