- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我为使用 GridLayoutManager
的 RecyclerView
创建了自定义 ItemDecoration
。 ItemDecoration
本质上确保在 RecyclerView
中应用所有 subview 之间的等效间距:
ItemDecoration
完全按照我希望的方式工作,我认为它看起来很棒。但是,我注意到在为我的 RecyclerView
设置布局管理器之前,我需要添加 ItemDecoration
。我的主要问题是:这是为什么?
我正在处理一些遗留代码,这些代码使用 CursorLoader
从网络中提取 RSS 提要并将它们显示给最终用户。无论出于何种原因,布局管理器都在 onLoadFinished()
中设置:
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
Adapter adapter = new Adapter(cursor);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager =
new GridLayoutManager(this, mColumnCount, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(gridLayoutManager);
}
我注意到,如果我在 onLoadFinished()
中添加我的 ItemDecoration
,项目之间的边距看起来比实际应该的要大:
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
Adapter adapter = new Adapter(cursor);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager =
new GridLayoutManager(this, mColumnCount, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(gridLayoutManager);
// Adding the custom ItemDecoration
EqualOffsetItemDecoration itemDecoration = new EqualOffsetItemDecoration(this, R.dimen.card_view_margin, mColumnCount);
mRecyclerView.addItemDecoration(itemDecoration);
}
上面的屏幕截图显示的边距比我预期的要多得多,因为我只应用了 8dps(card_view_margin
的值)。但是,如果我在 onCreate()
中添加 ItemDecoration
,那么它会按预期显示:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mColumnCount = getResources().getInteger(R.integer.list_column_count);
/*
In order to have equal spacing along the edges of the screen as well as between the
child views of the RecyclerView, an EqualOffsetItemDecoration is applied to the RecyclerView.
*/
EqualOffsetItemDecoration itemDecoration = new EqualOffsetItemDecoration(this, R.dimen.card_view_margin, mColumnCount);
mRecyclerView.addItemDecoration(itemDecoration);
...
}
...第一个屏幕截图就是这种情况。那么为什么这很重要呢?为什么我需要在将布局管理器应用到我的 RecyclerView
之前添加 ItemDecoration
?我确信这与幕后执行的顺序有关。非常感谢任何类型的解释:)
仅供引用,以防有人对我如何创建 ItemDecoration
感兴趣,这里是:
最佳答案
有趣的问题,但我认为在项目装饰之前或之后设置布局管理器并不重要。这两个调用都会导致布局请求。
我猜测您不止一次向 RecyclerView
添加装饰。由于装饰复合,您会看到装饰添加两次(或更多次)而不是一次的更大差距。
关于android - LayoutManager 之前的 ItemDecoration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968085/
根据谷歌的 RecyclerView documentation ,您可以通过在 RecyclerView 的“layoutManager”属性中指定其类名,从布局文件中设置特定的 LayoutMan
运行实例化此类的代码时: static final class MyFrame extends JFrame { private CardLayout layout = new CardLay
我有一个带有卡片列表的 RecyclerView。我想知道是否可以通过编程将 RecyclerView 的 LayoutManager 更改为 Linear(在使用手机时)和 StaggeredGri
我在尝试为我的 recyclerview 设置布局管理器时遇到此错误。我已经创建了一个布局管理器并设置了我的 recyclerview 来使用它。由于某种原因,layoumanager 为空。 jav
前段时间我读到这个article显示了一种在 Swing 中实现鼠标可调整大小的组件的方法。 作者使用空 LayoutManager 以允许绝对组件定位。我知道不应该使用 null 布局,所以我的问题
请看附图。我是 Java GUI 的初学者,希望有人能指导我为这样的 GUI 选择布局管理器。我知道我可能必须使用嵌套的布局管理器,但我不确定哪个可以帮助我完成这项工作。 最佳答案 这是一个想法: 在
我想像这样实现一个 ConcatAdapter : 此页面的 recyclerView 包含以下元素: 标题 水平recyclerView(带分页) 标题 垂直recyclerView(无分页) 标题
所以,我对 LayoutManager 完全陌生。我已经阅读过有关现有 LayoutManager 的信息,但没有了解最好的方法是获得这样的布局。不同的颜色框代表 JPanel。 我应该使用哪一个?
我正在学习在 GUI 中定位对象的不同方法,但我不太明白为什么需要转换为 LayoutManager 才能使用 setLayout 方法。是否需要实现 LayoutManager 接口(interfa
我正在尝试对 UITextView 的 layoutManager 进行子类化 但不断发生崩溃。我一直在看的示例是 Objective C 中的,所以我对如何更改布局管理器感到有点困惑 这是我的代码.
我正在开发一个应用程序,并且有一个需要自定义布局管理器的面板。为了测试我的 LayoutManager 实现,我希望内置一些 Debug模式,非常类似于 MigLayout 的 Debug模式,它在
我正在尝试在我的应用程序中包含一个 SwipeRefreshLayout。它可以工作,但是当我关闭我的 WIFI 时(我的应用程序使用它,如果没有 wifi,我想包括警报)。任何人有任何建议为什么我得
我有一段文字,它在屏幕上的显示完全像这样: #first SomePersonFirstName SomePersonLastName #secondTag 在我的应用程序中,我保留了有关此文本的 3
如何防止 GUI 组件在 BorderLayout 管理器中调整大小时缩小?是否可以设置最小尺寸以使组件不会缩小超过它?似乎使用 setBounds 不适用于布局管理器。 还有一个问题:如果我使用嵌套
我无法找到为什么我们需要为 RecycleView 设置 LayoutManager 的解释。有人可以解释吗? mLayoutManager = new LinearLayoutManager(thi
我正在尝试编译这个非常简单的 BoxLayout 管理器测试,但我不断收到此错误:“Container 类型中的方法 setLayout(LayoutManager) 不适用于我的行面板上的参数 (L
我想利用 JFrame 的 Container 的默认布局:LayoutManager,避免使用 专门化它>FlowLayout ¹. 所以我写了这段代码: LayoutManager layout_
我用这样的 StaggeredGridLayoutManager 制作了 RecyclerView: 我需要在上面添加一些 TextView(像 LinearLayoutManager 中的一个列)。
我正在尝试构建一个涉及 RecyclerView 或 GridView 的 UI,我想在其中根据动态填充的项目的数量和宽度自动设置列数。 这是我到目前为止所取得的成就的形象...... 这是我想要实现
我正在使用 RecyclerView 和 StaggeredGridLayoutManager。 我希望这个 StaggeredGridLayoutManager 或任何一个 LayoutManage
我是一名优秀的程序员,十分优秀!