【问题标题】:How to manage empty span in GridLayoutManager?如何在 GridLayoutManager 中管理空跨度?
【发布时间】:2017-08-29 05:33:11
【问题描述】:

我正在使用网格布局管理器在部分 recyclerview 中设置联系人目录,我的问题是如果跨度为空,标题也被设置为跨度中的一个项目。

我尝试使用 SpanSizeLookup 方法。它没有像我预期的那样工作。

layoutManager = new GridLayoutManager(getActivity(), 3);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(adapterDocument.getItemViewType(position)){
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 3;

                    case SectionedRecyclerViewAdapter.VIEW_TYPE_ITEM_LOADED:
                        return 1;
                    default:
                        return 1;
                }
            }
        });

这就是我得到的

这是我真正想要的:

如何使标题应该在下一行全宽?谢谢。

【问题讨论】:

    标签: android android-recyclerview gridlayoutmanager sectionedrecyclerviewadapter


    【解决方案1】:

    您的代码有问题,我将onCreateView 中的代码从Example1 更改为:

        sectionAdapter = new SectionedRecyclerViewAdapter();
    
        for(char alphabet = 'A'; alphabet <= 'Z';alphabet++) {
            List<String> contacts = getContactsWithLetter(alphabet);
    
            if (alphabet == 'B' || alphabet == 'D') {
                contacts = Collections.emptyList();
            }
    
            if (contacts.size() > 0) {
                sectionAdapter.addSection(new ContactsSection(String.valueOf(alphabet), contacts));
            }
        }
    
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(sectionAdapter.getSectionItemViewType(position)) {
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 3;
                    default:
                        return 1;
                }
            }
        });
        recyclerView.setLayoutManager(glm);
        recyclerView.setAdapter(sectionAdapter);
    

    它工作正常,结果如下:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      相关资源
      最近更新 更多