[英]RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager
我基本上是在尝试实现这一设计原则( 来自Google的Material Design ): 因此,我已经使用LinearLayoutManager
制作了一个父RecyclerView
,然后在RecyclerView
适配器中为“富媒体”部分(操作区域2)添加了一个带有GridLayoutManager
的子RecyclerView
。 一切正常,除了我将内部RecyclerView
网格设置为具有match_parent
宽度和wrap_content
高度的事实外,但它无法正确计算内容的大小,似乎将其保留为0并因此被隐藏了。 如果我将子级RecyclerView
设置为特定高度,则项目会显示在其中,但是当然会在底部将其切除。 其他人似乎也遇到了这个问题, 但是在他们的情况下,它们都具有线性布局 。 (另请参阅此处的“凯”的答案 。)
现在我的问题是,如何在上面链接的问题的公认答案中,像在“ pptang”中那样覆盖onMeasure
方法,但是要在自定义GridLayoutManager
而不是自定义LinearLayoutManager
? 我没有在这里发布我的代码,因为它基本上与链接的代码相同,只是我需要为子级RecyclerView
制作一个自定义GridLayoutManager
,以便它可以正确地作为“ pptang's”答案状态进行度量。
否则,有没有比在第二个RecyclerView中使用1个RecyclerView更好的方法? 只能有1个RecyclerView用上面的CardViews
列表和每个CardView
的唯一项网格填充活动/片段吗?
您可以使用SectionedRecyclerViewAdapter库仅使用一个RecyclerView来构建它。
您可以在下面找到图像示例的完整代码。
首先创建一个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);
总结一下。 您不应该在回收站内部使用回收站。 您需要实现一个自定义的gridLayoutManager。 为此,请阅读以下内容:
从文档https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html
ItemDecoration允许应用程序从适配器的数据集向特定的项目视图添加特殊的图形和布局偏移。 这对于在项目,突出显示, 可视分组边界等之间绘制分隔线很有用。
因此,如果将以上内容与此http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html结合使用,您肯定可以实现所需的功能。 只需将您从材质指南中呈现的图像想象成一个在gridLayoutManager中的组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.