gpt4 book ai didi

android - RecyclerView with GridLayoutManager in RecyclerView with LinearLayoutManager

转载 作者:搜寻专家 更新时间:2023-11-01 08:32:33 26 4
gpt4 key购买 nike

我基本上是在尝试实现这个设计原则(from Google's Material Design): enter image description here因此,我创建了一个带有 LinearLayoutManager 的父级 RecyclerView,然后在 RecyclerView 适配器中,我将子级 RecyclerView 和用于“富媒体”部分(操作区域 2)的 GridLayoutManager。一切正常,除了我将内部 RecyclerView 网格设置为具有 match_parent 宽度和 wrap_content 高度,但它不会正确计算内容的大小,似乎将其保留为 0 并因此隐藏。如果我将子 RecyclerView 设置为特定高度,项目会显示在其中,但当然会在底部被切断。其他人似乎也遇到过这个问题,but in their case, both have linear layouts。 (另见 "Khay's" answer here 。)

现在我的问题是,如何像“pptang”在上面链接问题的接受答案中所做的那样覆盖 onMeasure 方法,而是在自定义 GridLayoutManager 中自定义 LinearLayoutManager?我没有在这里发布我的代码,因为它本质上与链接的代码相同,只是我需要为子 RecyclerView 创建一个自定义的 GridLayoutManager,这样它正确测量为“pptang 的”回答状态。

否则,有没有比在第二个 RecyclerView 中使用 1 个 RecyclerView 更好的方法?只能使用 1 个 RecyclerView 填充一个 Activity/fragment ,同时使用上述 CardViews 列表和每个 CardView 中的唯一项目网格?

最佳答案

您可以使用库 SectionedRecyclerViewAdapter 仅使用一个 RecyclerView 来构建它.

您可以找到下图示例的完整代码 here .

enter image description here

首先创建一个Section类:

class MySection extends StatelessSection {

String title;
String subtitle;
List<String> list;

public MySection(String title, String subtitle, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);

this.title = title;
this.subtitle = subtitle;
this.list = list;
}

@Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

// bind your view here
itemHolder.tvItem.setText(list.get(position));
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

// bind your header view here
headerHolder.tvTitle.setText(title);
headerHolder.tvSubTitle.setText(subtitle);
}
}

然后您使用您的 Sections 设置 RecyclerView:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MySection section1 = new MySection("Title", "Subhead", firstDataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);

关于android - RecyclerView with GridLayoutManager in RecyclerView with LinearLayoutManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39394213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com