【问题标题】:GridLayoutManager setSpanSizeLookup not workingGridLayoutManager setSpanSizeLookup 不起作用
【发布时间】:2017-01-30 04:17:23
【问题描述】:

我正在使用带有 2 个单元格的 GridLayoutManager,对于某些单元格,我希望 span 为一个单元格,因此我尝试使用 setSpanSizeLookup,但它不起作用。我尝试为所有位置返回跨度计数 1,但仍然出现两个单元格而不是一个。

以下是我的代码

gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return 1;
        }
    });
    recyclerView.setLayoutManager(gridLayoutManager);

有什么原因导致它不工作?

【问题讨论】:

    标签: android android-recyclerview android-support-library gridlayoutmanager


    【解决方案1】:

    替换

    return 1;
    

    return 2;
    

    这指定您将 2 个单元格跨入 1 个单元格。

    代码

    这是我为特定位置跨越 2 个单元格的代码

    GridLayoutManager glm=new GridLayoutManager(mContext,2);
    glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(categoryAdapter.getItemViewType(position)) {
                    case 1:
                        return 2;
                    default:
                        return 1;
                }
            }
        });
        mRecyclerViewCategory.setLayoutManager(glm);
    

    如何在您的 Recycler Adapter 中定义案例跨度

    @Override
    public int getItemViewType(int position) {
        if(position==[your_specific_postion_where_to_span]){
            return 1;
        }
        return super.getItemViewType(position);
    }
    

    【讨论】:

    • 我想在偶数位置有 2 个跨度,奇数位置有 1 个跨度,但是两个跨度只显示一个,并且第二列的网格空间只留空!我希望所有职位的行为都像这样,而不是特定的
    • @NaszNjokaSr.just modify getItemViewType if condition should be if(position%2 != 0){ return 1}; 其余代码同上
    • 它将在所有奇数位置跨越 1 列
    【解决方案2】:

    我为此苦苦挣扎,因为此时这里的文档很差。我是这样想的……

    getSpanSize 和 getSpanIndex 似乎可以一起工作。对我来说,我试图在跨越两列的 gridlayoutManager 中插入一个 pageViewer。所以它被定义为:mGridLayout = new GridLayoutManager(getActivity(), 2);

    //must be called before setLayoutManager is invoked
    private void setNumOfColumnsForPageViewer(final FallCollectionRecyclerAdapter adapter) {
    
        mGridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (adapter.getItemViewType(position) == MyRecyclerAdapter.TYPE_PAGE_VIEWER)
                    return 2; //some other form of item like a header, or whatever you need two spans for
                else
                    return 1; //normal item which will take up the normal span you defined in the gridlayoutmanager constructor
            }
    
            @Override
            public int getSpanIndex(int position, int spanCount) {
                if (adapter.getItemViewType(position) == FallCollectionRecyclerAdapter.TYPE_PAGE_VIEWER)
                    return 1;//use a single span
                else
                    return 2; //use two spans
            }
        });
    
        mRecyclerView.setLayoutManager(mGridLayout);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多