简体   繁体   English

使用 RecyclerView 跨越多列

[英]Span multiple columns with RecyclerView

So what I am trying to go for is having a staggered layout but the first item in the list needs to span two columns.所以我想要的是交错布局,但列表中的第一项需要跨越两列。 The first two rows are also a fixed height.前两行也是固定高度。 I have everything working except the first item spanning two columns.除了跨越两列的第一项之外,我一切都在工作。

I am using the RecyclerView.Adapter with the StaggeredGridLayoutManager.我将 RecyclerView.Adapter 与 StaggeredGridLayoutManager 一起使用。 Doesn't seem like it is out of the box functionality.它似乎不是开箱即用的功能。 I assume I can make a custom layout manager I'm just not sure where to start.我想我可以制作一个自定义布局管理器,只是不知道从哪里开始。 I have tried searching but I can't find anything about how to get items to span multiple columns.我尝试过搜索,但找不到有关如何让项目跨越多列的任何信息。

The image below is what what I am looking for in the list.下图是我在列表中寻找的内容。

交错布局

Currently, StaggeredGridLayoutManager only supports views that span all the columns (for a vertically configured layout), and not an arbitrary number of them.目前, StaggeredGridLayoutManager仅支持跨越所有列的视图(对于垂直配置的布局),而不是任意数量的列。

If you still want to span them across all the columns, you should do this in the adapter implementation:如果您仍希望将它们跨越所有列,则应在适配器实现中执行此操作:

public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
    layoutParams.setFullSpan(true);
}

IMHO, StaggeredGridLayoutManager is still under heavy development and Google wants us to use it for feedback :(恕我直言, StaggeredGridLayoutManager仍在大量开发中,谷歌希望我们将其用于反馈:(

What I did to span all columns for a vertically configured layout was create new LayoutParams and set full span to true in the adapter implementation:我为垂直配置的布局跨越所有列所做的是创建新的LayoutParams并在适配器实现中将 full span 设置为 true:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {

    StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.setFullSpan(true);
    viewHolder.itemView.setLayoutParams(layoutParams);
}

The solution proposed by ljmelgui works fine but can be mixed with mato answer for a minor optimization by trying to reuse the layoutParams in case they exist:提出的解决方案ljmelgui工作正常,但可与混合马托格罗索答案试图重用的情况下,它们的存在对的LayoutParams未成年优化:

public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
    if ( layoutParams == null ) {
          layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
    layoutParams.setFullSpan(true);
    viewHolder.itemView.setLayoutParams(layoutParams);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM