【问题标题】:Recyclerview Dynamic Sections not using any 3rd libRecyclerview 动态部分不使用任何第三个库
【发布时间】:2016-03-27 11:10:19
【问题描述】:

我想将标题添加到回收视图中,我正在尝试使用

来实现它
 @Override
public int getItemViewType(int position) {
    // depends on your problem
    if (position == 0 || position == 4) {
        return 2;
    } else {
        return 1;
    }
}

 @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {
    // create a new view
    if (viewType == 1) {
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row, null);
        return new ViewHolder(itemLayoutView);

    } else if (viewType == 2) {
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row1, null);
        return new ViewHolders(itemLayoutView);
    }


    return null;


}

但是我如何在运行时做到这一点,比如当我不知道它应该显示部分的位置时,就像我有 json

{
"DATA": [
    "hi",
    "heloo",
    "bye"
],
"MAHA": [
    "ans",
    "rs",
    "re",
    "jab",
    "bad"
]

}

data 和 maha 是我想要显示其他元素的部分

目前我正在制作所有元素的数组列表并添加核心 部分的值,但我如何使用上面的 json 来做到这一点

【问题讨论】:

  • 使用您当前的设计,您可以在适配器中保留一个成员数组或标题位置列表,如果位置值在数组/列表中,则从 getItemViewType() 返回类型 2。为它创建一个setter方法,在解析JSON的时候算出来,在调用notifyDataSetChanged()之前设置好。

标签: android android-recyclerview


【解决方案1】:

您需要的是一个包含父项(标题)和子项(条目)的可扩展回收视图。干得好: https://github.com/bignerdranch/expandable-recycler-view

如果您想始终显示条目并且不从可扩展功能中获利,只需执行以下操作:expAdapter.expandAllParents()。

我知道您不想使用 3rd 图书馆聚会,但在我看来,这是处理它的最佳方式,可以节省您大量时间。此外,如果其他人有同样的问题,他可能会发现这个解决方案很有用。

【讨论】:

    【解决方案2】:

    为您的 JSON 数据创建一个这样的类,并为每个节点创建一个 Section 类(在您的示例中,一个用于 DATA,另一个用于 MAHA):

    class Section {
        String header;
        List<String> itemList;
    }
    

    然后创建您的自定义适配器:

    public class SectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<Section> sectionList;
    
        public SectionedRecyclerViewAdapter(List<Section> sectionList) {
            this.sectionList = sectionList;
        }
    
        @Override
        public int getItemViewType(int position) {
            int currentPos = 0;
    
            for (Section section : sectionList) {
                // check if position is in this section
                // +1 for the header, -1 because idx starts at 0
                if (position >= currentPos && position <= (currentPost + section.itemList.size() + 1 - 1)) {
                    if (position == currentPos) {
                        return 2; // header
                    } else {
                        return 1; // item
                    }
                }
    
                // +1 for the header
                currentPos += section.itemList.size() + 1;
            }
    
            throw new IndexOutOfBoundsException("Invalid position");
        }
    
        // use the onCreateViewHolder method from your question...
    }
    

    我知道您不想使用 3rd 方库,但您可以检查 SectionedRecyclerViewAdapter 的 getItemViewType 方法是如何完成的 here

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多