Friday 27 June 2014

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 vieVIEW.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.
@Override
public int getViewTypeCount() {
return 4;
}
2) getItemViewType() :-
@Override
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.
@Override
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
.findViewById(R.id.list_raw_map);
convertView.setTag(mapViewHolder);
} else {
mapViewHolder = (MapViewHolder) convertView.getTag();
}
GooglePlaySmallCard cardGmap = new GooglePlaySmallCard(mContext);
cardGmap.setId("gplaysmall");
// Set card in the cardView
mapViewHolder.cardViewGmap.setCard(cardGmap);
} 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
.findViewById(R.id.list_carddemo_shadow_layout);
convertView.setTag(hdrViewHolder);
} else {
hdrViewHolder = (HdrViewHolder) convertView.getTag();
}
Card shcard = new Card(mContext);
// Create a CardHeader
CardHeader header = new CardHeader(mContext);
// Set the header title
header.setTitle(mContext.getString(R.string.demo_header_basetitle));
shcard.addCardHeader(header);
// Hidden shadow
shcard.setShadow(true);
hdrViewHolder.cardshd.setCard(shcard);
} 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
.findViewById(R.id.list_carddemo_Gplay1);
gridViewHolder.gdcardView1 = (CardView) convertView
.findViewById(R.id.list_carddemo_Gplay2);
convertView.setTag(gridViewHolder);
} else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
GplayCard gcard = new GplayCard(mContext);
gridViewHolder.gdcardView.setCard(gcard);
gridViewHolder.gdcardView1.setCard(gcard);
} 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
.findViewById(R.id.list_carddemo_cardBirth);
convertView.setTag(brthViewHolder);
} else {
brthViewHolder = (BrthViewHolder) convertView.getTag();
}
GoogleNowBirthCard birthCard = new GoogleNowBirthCard(mContext);
birthCard.setId("myId");
brthViewHolder.bthcardView.setCard(birthCard);
}
return convertView;
}

 Hope this is useful to someone.

29 comments:

  1. Perfect. I am going to try this later and comment if it works.

    ReplyDelete
  2. can you please send me source code aazamkhan976@gmail.com

    ReplyDelete
    Replies
    1. not valid lucianoblz@yahoo.com

      Delete
  3. Can it be used to swithch view according to the selected list item?

    ReplyDelete
    Replies
    1. yet i havnt try but it may be possible using check value of selected item.

      Delete
  4. Please send me the full source code emmacodos@yahoo.com. Thanks

    ReplyDelete
  5. Hi, I needed this code. Please send me source code huydungktv@gmail.com. Thanks

    ReplyDelete
  6. great, please send me the full source code, Thanks!!!
    duffz@gmx.li

    Happy new 2015 :)

    ReplyDelete
  7. Hola que tal me has ayudado mucho con este tutorial, tenia que montar items distintos en un listview. Muchas gracias amigo. SALUDOS

    ReplyDelete
  8. Hi, please send full source code janandjai@gmail.com . I have faced a issue related to classcastexception while scrolling listview

    ReplyDelete
  9. It is indeed useful. Glad you tried this, we can follow your steps.
    Thanks. Alex

    ReplyDelete
  10. Please send me the full source code kang6264@gmail.com Thanks

    ReplyDelete
  11. please send me source code kjmgganari@gmail.com

    ReplyDelete
  12. Hello!!

    thank you for your help! World need more people like you. Could you send me the full code to rogerabino@gmail.com ?

    ReplyDelete
  13. you saved my life. thanks a lot

    ReplyDelete
  14. I cant understand this part:
    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;
    }

    can you explain me?
    Thanks

    ReplyDelete
    Replies
    1. As I understand, he has 4 differents Layouts, I don´t know exactly if TextModel, HeaderModel and the others are layouts, but they could be, for instance, if TextModel is a custom layout that extends from LinearLayour, anyway.
      He is checking if the item in that position belong to that type, then return the corresponding row type and inflate the correct layout.

      Delete
  15. please send me source code navyarn14@gmail.com,Thanks.

    ReplyDelete
  16. Hi Please send me source code on neeraj.g.25612@gmail.com

    ReplyDelete
  17. Cool, please send me the source code, Thanks
    manu.badoni@gmail.com

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Kamaliya, very happy that I have come across your tutorial. It has made my day. I was confused about using getItemType and getItemTypeCount function. You have made my mind clear now. May God bless you

    ReplyDelete
  20. Please my i have the source code on lucianoblz@yahoo.com .Thank you

    ReplyDelete
  21. please send m the source code,Thanks
    bhagyashrigaikwad1@gmail.com

    ReplyDelete
  22. please send source code on substar55@yahoo.com

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. hi, please send me source code on m-randa@hotmail.fr

    ReplyDelete
  25. Hi , Can you please send the source code on mbabhi92@gmail.com

    ReplyDelete

Copy and share your useful data from Google chrome browser to your application in android.

Here In this blog I explain that how to copy and share your useful data from chrome browser in android. 1) How to show your applic...