Margin between items in recycler view Android

后端 未结 10 1330
自闭症患者
自闭症患者 2020-12-24 02:06

Hi I am Following this tutorial:

http://www.journaldev.com/10024/android-recyclerview-and-cardview-example-tutorial

Now I am facing a weird issue the mar

相关标签:
10条回答
  • 2020-12-24 02:15

    I faced similar issue, with RelativeLayout as the root element for each row in the recyclerview.

    To solve the issue, find the xml file that holds each row and make sure that the root element's height is wrap_content NOT match_parent.

    0 讨论(0)
  • 2020-12-24 02:19

    change in Recycler view match_parent to wrap_content:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    Also change in item layout xml

    Make parent layout height match_parent to wrap_content

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    
    0 讨论(0)
  • 2020-12-24 02:21

    I made a class to manage this issue. This class set different margins for the items inside the recyclerView: only the first row will have a top margin and only the first column will have left margin.

    public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
    private final int columns;
    private int margin;
    
    /**
     * constructor
     * @param margin desirable margin size in px between the views in the recyclerView
     * @param columns number of columns of the RecyclerView
     */
    public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0) int columns ) {
        this.margin = margin;
        this.columns=columns;
    
    }
    
    /**
     * Set different margins for the items inside the recyclerView: no top margin for the first row
     * and no left margin for the first column.
     */
    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
    
        int position = parent.getChildLayoutPosition(view);
        //set right margin to all
        outRect.right = margin;
        //set bottom margin to all
        outRect.bottom = margin;
        //we only add top margin to the first row
        if (position <columns) {
            outRect.top = margin;
        }
        //add left margin only to the first column
        if(position%columns==0){
            outRect.left = margin;
            }
        }
    }
    

    you can set it in your recyclerview this way

     RecyclerViewMargin decoration = new RecyclerViewMargin(itemMargin, numColumns);
     recyclerView.addItemDecoration(decoration);
    
    0 讨论(0)
  • 2020-12-24 02:21
    1. Find the attribute card_view:cardUseCompatPadding="true" in cards_layout.xml and delete it. Start app and you will find there is no margin between each cardview item.

    2. Add margin attributes you like. Ex:

      android:layout_marginTop="5dp"
      android:layout_marginBottom="5dp" 
      
    0 讨论(0)
  • 2020-12-24 02:23

    Use CompatPadding in CardView Item

    0 讨论(0)
  • 2020-12-24 02:28

    Instead of using XML to add margin between items in RecyclerView, it's better way to use RecyclerView.ItemDecoration that provide by android framework.

    So, I create a library to solve this issue.

    https://github.com/TheKhaeng/recycler-view-margin-decoration

    0 讨论(0)
提交回复
热议问题