2

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

在此处输入图像描述

如何动态更改 StaggeredGridLayout 的行跨度。@Gabriele Mariotti 建议了一种实现

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

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

这是我的适配器类的代码,它将回收器适配器与视图持有者类一起扩展为嵌套类

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);

    }
}
}
4

1 回答 1

3

对于一个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);
}
于 2018-05-10T23:53:15.347 回答