Span multiple columns with RecyclerView

后端 未结 3 555
故里飘歌
故里飘歌 2020-12-07 12:37

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 every

相关标签:
3条回答
  • 2020-12-07 13:03

    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:

    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);
    }
    
    0 讨论(0)
  • 2020-12-07 13:11

    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:

    @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);
    }
    
    0 讨论(0)
  • 2020-12-07 13:13

    Currently, StaggeredGridLayoutManager only supports views that span all the columns (for a vertically configured layout), and not an arbitrary number of them.

    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 :(

    0 讨论(0)
提交回复
热议问题