Ayush Aggarwal
Ayush Aggarwal

Reputation: 225

How do I implement Dynamic Row Span in StaggeredGridLayoutManager

I am trying to achieve customn Rowspan in StaggeredGrid

enter image description here

How do I dynamically change the rowspan of my StaggeredGridLayout . There is one implementation suggested by @Gabriele Mariotti

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

But I am not able to get it to work. Please suggest me some ways to implement this.

Here is the code to my Adapter Class that extends the Recycler Adapter along with the View holder class as a nested Class

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

    }
}
}

Upvotes: 2

Views: 3602

Answers (1)

Mike
Mike

Reputation: 94

For a StaggeredGridLayoutManager you might try something like this:

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

Upvotes: 3

Related Questions