简体   繁体   中英

How do I space out items in a recyclerview?

Hey how do I space out items in a recyclerview, I am currently using an array in java to provide the images and the names of the objects, however, it wont let me use padding to space out the items in my list??

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.athena.athenafront.NavigationFragment"
                xmlns:app="http://schemas.android.com/apk/res-auto"

                android:background="@android:color/white">
<!-- TODO: Update blank fragment layout -->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primaryColor"
            />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawer_recyclerview"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:choiceMode="singleChoice"
        android:dividerHeight="0dp"
        android:background="#ffff"
        android:layout_height="400dp">
    </android.support.v7.widget.RecyclerView>

Here is my java

public static List<AthenaPanel> getData() {
    //created an object for ur Drawer recyclerview array
    List<AthenaPanel> data= new ArrayList<>();
    //this is where you would add all your icons for the drawer list
    //arrays of icons
    int[] icons={R.mipmap.ic_launcher};
    String[] titles = {"Explore","Myprofile","MyStatus","Calendar","Setting","Send FeedBack"};
    //this is our for loop to cycle through the icons and the titles
    for(int i=0;i<5;i++){
        AthenaPanel current=new AthenaPanel();
        //i%().length allows ups to loop over the array any number of times we want to
        current.iconId=icons[i%icons.length];
        current.title=titles[i%titles.length];
        data.add(current);
    }
    return data;
}

To add the space on the RV itself do this:

On your RecyclerView xml, use this: android:padding="8dp" or any other value that you like. Along with android:clipToPadding="false" .

Eg:

<android.support.v7.widget.RecyclerView
    android:id="@+id/drawer_recyclerview"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:choiceMode="singleChoice"
    android:dividerHeight="0dp"
    android:background="#ffff"
    android:layout_height="400dp"
    android:clipToPadding="false"
    android:padding="8dp">
</android.support.v7.widget.RecyclerView>

More info on that here .


To add space on the RV items, you'll have to edit the XML files of your item row and add the margin/padding there.

EDIT: Maybe this question is related.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM