StaggeredGridLayoutManager

suggest change
  1. Create your RecyclerView in your layout xml file:
<android.support.v7.widget.RecyclerView
            android:id="@+id/recycleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
  1. Create your Model class for holding your data:
public class PintrestItem {
        String url;
        public PintrestItem(String url,String name){
            this.url=url;
            this.name=name;
        }
        public String getUrl() {
            return url;
        }
    
       public String getName(){
           return name;
       }
        String name;
    }
  1. Create a layout file to hold RecyclerView items:
<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/imageView"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/name"
            android:layout_gravity="center"
            android:textColor="@android:color/white"/>
  1. Create the adapter class for the RecyclerView:
public class PintrestAdapter extends  RecyclerView.Adapter<PintrestAdapter.PintrestViewHolder>{
   private ArrayList<PintrestItem>images;
   Picasso picasso;
   Context context;
   public PintrestAdapter(ArrayList<PintrestItem>images,Context context){
       this.images=images;
       picasso=Picasso.with(context);
       this.context=context;

   }

   @Override
   public PintrestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.pintrest_layout_item,parent,false);
       return new PintrestViewHolder(view);
   }

   @Override
   public void onBindViewHolder(PintrestViewHolder holder, int position) {
         picasso.load(images.get(position).getUrl()).into(holder.imageView);
         holder.tv.setText(images.get(position).getName());
   }

   @Override
   public int getItemCount() {
       return images.size();
   }

   public class PintrestViewHolder extends RecyclerView.ViewHolder{
       ImageView imageView;
       TextView tv;
       public PintrestViewHolder(View itemView) {
           super(itemView);
           imageView=(ImageView)itemView.findViewById(R.id.imageView);
           tv=(TextView)itemView.findViewById(R.id.name);
           
       }
   }
}
  1. Instantiate the RecyclerView in your activity or fragment:
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Create the instance of StaggeredGridLayoutManager with 2 rows i.e the span count and provide the orientation
StaggeredGridLayoutManager layoutManager=new new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
// Create Dummy Data and Add to your List<PintrestItem>
List<PintrestItem>items=new ArrayList<PintrestItem>
items.add(new PintrestItem("url of image you want to show","imagename"));
items.add(new PintrestItem("url of image you want to show","imagename"));
items.add(new PintrestItem("url of image you want to show","imagename"));
recyclerView.setAdapter(new PintrestAdapter(items,getContext() );

Don’t forgot to add the Picasso dependency in your build.gradle file:

compile 'com.squareup.picasso:picasso:2.5.2'

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents