sách gpt4 ăn đã đi

RecyclerView 中的 Android RecyclerView?

In lại 作者:搜寻专家 更新时间:2023-11-01 09:45:01 27 4
mua khóa gpt4 giày nike

我正在使用显示不同类别列表的 RecyclerView。每行项目还包含 RecyclerView 以显示类别项目列表。父级 RecyclerView 由垂直 LinearLayoutManager 填充,子级 RecyclerViews phụ thuộc vào GridLayoutManager 填充(2 列)。子 RecyclerViews 不显示除前 2 个之外的所有类别项目,即使它有很多类别项目。所有剩余的类别项目都隐藏在特定类别中。换句话说,您可以说子级 RecyclerView 没有扩展到它包含的项目那么多。

我在这里找到了不同的解决方案,但没有人奏效。这就是为什么再次发帖。

câu trả lời hay nhất

您可以使用这个 thư viện 仅用一个 RecyclerView 轻松实现它.见下图:

nhập mô tả hình ảnh ở đây

首先创建一个Section类:

class MySection extends StatelessSection {

Tiêu đề chuỗi;
List list;

public MySection(String title, List 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.list = list;
}

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

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

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

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

@Ghi đè
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new MyHeaderViewHolder(view);
}

@Ghi đè
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

// bind your header view here
headerHolder.tvItem.setText(title);
}
}

然后您使用您的 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("Categories 1", categories1DataList);
MySection section2 = new MySection("Categories 2", categories2DataList);

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

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Ghi đè
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
mặc định:
trả về 1;
}
}
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);

关于RecyclerView 中的 Android RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484655/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress