- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我基本上是在尝试实现这个设计原则(from Google's Material Design): 因此,我创建了一个带有 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 .
首先创建一个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/
我正在尝试制作类似于 Instagram 过滤器布局的布局。基本上,当您选择一个过滤器时,它会滚动到您选择的项目 + 1,向您显示还有更多过滤器。 我目前正在尝试为我的水平 RecyclerView
在 GridLayoutManager 中,我能够设置跨度计数,这使得 View 内的项目可以调整大小,以便能够水平适应该跨度计数。 我有一个 LinearLayoutManager,我想以同样的方式
我有一个在 fragment 中实现的 RecyclerView,我正在尝试向代码中添加分隔符。我让 RecylcerView 按预期工作,但 LinearLayoutManager 无法解析 get
我需要修改与 RecyclerView 一起使用的垂直 LinearLayoutManager 以便它可以水平滚动。 当 RecyclerView 垂直滚动时,它会添加新项目,因为我正在扩展 Line
我的 Activity 正在显示一个带有 LinearLayoutManager 的 RecyclerView。我想获取列表中的第一项 (View)。根据this post's accepted an
我正在尝试做这样的事情: RecyclerAdapter recyclerAdapter; LinearLayoutManager recyclerManager; //set items.... r
我的 LinearLayoutManager 有一个错误,当我运行这段代码时它说 error: incompatible types: NotificationFragment cannot be c
我想将 RecyclerView 滚动到某个位置,我使用 LinearLayoutManager.scrollToPositionWithOffset() 滚动,但它不起作用。谁能帮帮我? 我的代码:
我开始使用 Kotlin 构建一个电影数据库应用程序,这是基于我在网上找到的教程(详见 this link。在构建电影 fragment 文件时,我注意到一个“未解析的引用:LinearLayoutM
我想要一个顶部有标题的 ListView 样式的 RecyclerView,并且我不希望标题 View 被回收,永远。 我对 RecyclerView 和 LinearLayoutManager 是全
我正在尝试新的 RecyclerView 结合 LayoutManager 和 LinearLayoutManager 的标准实现。我的目标是让 LayoutManager 在设备处于纵向时水平绘制我
我正在使用 RecyclerView 和相应的 LinearLayoutManager。我在我的应用程序的一部分中添加了一些自定义滚动功能,我将在其中翻译自定义 header 对象以及根据此项目建模的
我基本上是在尝试实现这个设计原则(from Google's Material Design): 因此,我创建了一个带有 LinearLayoutManager 的父级 RecyclerView,然后
除了这两个,我可以从 Android 库中导入其他所有内容。我正在尝试从 android.support.v7.widget 导入它们,但这不起作用,但是从 android.support.v4.wi
这似乎是一个简单的解决方案,但似乎设置 private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; priva
目前我正在使用以下代码来检查是否应该启用 SwipeRefreshLayout。 private void laySwipeToggle() { if (mRecyclerView.getCh
我有一个项目列表。这些项目按日期排序。日期早于现在的项目(“旧”项目)在用户打开应用程序时不应该可见,但用户应该能够通过将列表滚动到顶部来查看它们。 我找到第一个带有 future 日期的项目("ne
IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView 当我尝试为我的 Recycler
我有一个 fragment ,其中有 RecyclerView 和 LinearLayoutManager,其中有 CardView 项。单击项目应滚动到顶部有一个 float 操作按钮。我已经尝试使
是否可以与 RecyclerView 中的项目重叠? 我正在尝试使用 LinearLayoutManager。 我的要求与 LinearLayoutManager 中的要求相同,只需将内部的项目从上到
我是一名优秀的程序员,十分优秀!