setSpanSizeLookup中用于recyclerView的列的间距

时间:2018-07-11 09:31:47

标签: android android-recyclerview

我正在使用recyclerViewCategories.setLayoutManager动态设置列数

代码:

    /** Initilize the GridView **/
    private void initGridView() {
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);
        recyclerViewCategories.setLayoutManager(mGridLayoutManager);
        recyclerViewCategories.setItemAnimator(new DefaultItemAnimator());
    }


    GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return categoryHomeModelList[position].getDisplayType().equals("0") ? 2 : 1;
        }
    };

enter image description here

我要解决的问题:垂直间距大于水平间距...如何减小列之间的垂直间距

1 个答案:

答案 0 :(得分:0)

尝试一下

对于RecyclerView

<android.support.v7.widget.RecyclerView
    android:id="@+id/rcy_View"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:padding="5dp" />  <== For side padding of list 

对于Adapter,假设您正在使用LinearLayout,则:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="5dp">  <=  This is for each item spacing in list
    .....
    .....
</LinearLayout>

这将为每个项目提供10dp的间距。无需更改列也可以使用。