Tuesday, January 6, 2015

using LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager with RecyclerView


In the Previous Post, We have created a Recycler View with list of items like listview,

Now we will see how simple to make a gridLayout and |StaggerdGrid with Recycler View by simply modifying the code in the method  to show the items in different ways in the UI .

 mRecyclerView.setLayoutManager(glm);




LinearLayoutManager
=================


mRecyclerView = (AutofitRecyclerView) findViewById(R.id.my_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

LinearLayoutManager llm = new LinearLayoutManager(this);

// use a linear layout manager to show items like listview
mRecyclerView.setLayoutManager(llm);

// create an Object for Adapter
mAdapter = new CardViewDataAdapter(myDataset);

// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);


ScreenShot:





















GridLayoutManager 
================

mRecyclerView = (AutofitRecyclerView) findViewById(R.id.my_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// first param is context and second param is spanCount ie.,
GridLayoutManager glm=new GridLayoutManager(this,2);

// use a grid layout manager to show items like gridview
mRecyclerView.setLayoutManager(glm);

// create an Object for Adapter
mAdapter = new CardViewDataAdapter(myDataset);

// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);


Sreenshot:





















StaggeredGridLayoutManager 
========================


mRecyclerView = (AutofitRecyclerView) findViewById(R.id.my_recycler_view);


// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);


// use a staggerd grid layout manager to show items based on the content width and height.
// first param is spanCount and second param is orientation ie., Vertical or Horizontal
StaggeredGridLayoutManager sglm=new StaggeredGridLayoutManager(2,1); // use a linear layout managermRecyclerView.setLayoutManager(sglm); // create an Object for AdaptermAdapter = new CardViewDataAdapter(myDataset); // set the adapter object to the RecyclerviewmRecyclerView.setAdapter(mAdapter);

Sreenshot:



No comments:

Post a Comment