【问题标题】:How do I implement Dynamic Row Span in StaggeredGridLayoutManager如何在 StaggeredGridLayoutManager 中实现动态行跨度
【发布时间】:2016-10-17 16:31:18
【问题描述】:

我正在尝试在 StaggeredGrid 中实现自定义 Rowspan

如何动态更改 StaggeredGridLayout 的行跨度。 @Gabriele Mariotti 提出了一种实现方式

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

但我无法让它工作。请建议我一些方法来实现这一点。

这是我的适配器类的代码,它将 Recycler Adapter 与 View holder 类一起扩展为嵌套类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
   LayoutInflater inflater;
Context context;
List<Information> data= Collections.emptyList();
public MyAdapter(Context context,List<Information>data)
{
    inflater= LayoutInflater.from(context);
    this.context=context;
    this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.custom_layout,parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current=data.get(position);
    holder.imageView.setImageResource(current.iconid);
    if(position==0) {
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        layoutParams.setFullSpan(true);
    }

}


@Override
public int getItemCount() {
    return 8;
}

class MyViewHolder extends RecyclerView.ViewHolder{
 ImageView imageView;
    public MyViewHolder(View itemView) {
        super(itemView);
        imageView= (ImageView) itemView.findViewById(R.id.image);

    }
}
}

【问题讨论】:

    标签: android android-recyclerview material-design staggeredgridlayout


    【解决方案1】:

    对于StaggeredGridLayoutManager,您可以尝试以下方法:

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final StaggeredGridLayoutManager.LayoutParams layoutParams =
            new StaggeredGridLayoutManager.LayoutParams(
                viewHolder.itemView.getLayoutParams());
        if (position == 0) {
            layoutParams.setFullSpan(true);
        } else {
            layoutParams.setFullSpan(false);
        }
        viewHolder.itemView.setLayoutParams(layoutParams);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2017-02-21
      • 2016-02-15
      • 2020-10-19
      • 2021-12-05
      • 2021-07-15
      • 2020-05-30
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多