Android: Multiple layout – use of getViewTypeCount() and getItemViewType() in Adapter

The use of getViewTypeCount() and getItemViewType() in Adapter:
If we need to show different type of view in list-view then its good to use  getViewTypeCount() and getItemViewType() in adapter instead of  toggling a view VIEW.GONE and VIEW.VISIBLE can be very expensive task inside getView() which will affect the list scroll.
So first of all you need to create different layouts for each type of view.
 1.simple textview.
 2.Row with image and text.
3.Simple row.
4.Row with two view .
So first need to create 4 different xmls for different row type.
Ho to use this getViewTypeCount() and getItemViewType() in adapter?
1) getViewTypeCount() :-
Returns the count of different type of views. Here we are having four different type of views so this method will return count as 4.
public int getViewTypeCount() {
return 4;
2) getItemViewType() :-
public int getItemViewType(int position) {

 if (mArrayList.get(position) instanceof TextModel) {
    return VIEW_TYPE_ROW_1;
 } else if (mArrayList.get(position) instanceof HeaderModel) {
    return VIEW_TYPE_ROW_2;
 } else if (mArrayList.get(position) instanceof ImageModel) {
    return VIEW_TYPE_ROW_3;
 } else {
    return VIEW_TYPE_ROW_4;
Now Do Work inside getView(…) method first we determine the type of view by calling getItemViewType(..) method, which will return the type to be inflated as shown below.
public View getView(int position, View convertView, ViewGroup parent) {
Holder.MapViewHolder mapViewHolder = null;
Holder.HdrViewHolder hdrViewHolder = null;
Holder.GridViewHolder gridViewHolder = null;
Holder.BrthViewHolder brthViewHolder = null;
int type = getItemViewType(position);
if (type == VIEW_TYPE_ROW_1) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.raw_one, null);
mapViewHolder = new Holder().new MapViewHolder();
mapViewHolder.cardViewGmap = (CardView) convertView
} else {
mapViewHolder = (MapViewHolder) convertView.getTag();
GooglePlaySmallCard cardGmap = new GooglePlaySmallCard(mContext);
// Set card in the cardView
} else if (type == VIEW_TYPE_ROW_2) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.raw_three, null);
hdrViewHolder = new Holder().new HdrViewHolder();
hdrViewHolder.cardshd = (CardView) convertView
} else {
hdrViewHolder = (HdrViewHolder) convertView.getTag();
Card shcard = new Card(mContext);
// Create a CardHeader
CardHeader header = new CardHeader(mContext);
// Set the header title
// Hidden shadow
} else if (type == VIEW_TYPE_ROW_3) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.raw_two, null);
gridViewHolder = new Holder().new GridViewHolder();
gridViewHolder.gdcardView = (CardView) convertView
gridViewHolder.gdcardView1 = (CardView) convertView
} else {
gridViewHolder = (GridViewHolder) convertView.getTag();
GplayCard gcard = new GplayCard(mContext);
} else if (type == VIEW_TYPE_ROW_4) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_demo_raw, null);
brthViewHolder = new Holder().new BrthViewHolder();
brthViewHolder.bthcardView = (CardView) convertView
} else {
brthViewHolder = (BrthViewHolder) convertView.getTag();
GoogleNowBirthCard birthCard = new GoogleNowBirthCard(mContext);
return convertView;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s