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.
@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.
Perfect. I am going to try this later and comment if it works.
ReplyDeletecan you please send me source code aazamkhan976@gmail.com
ReplyDeletenot valid lucianoblz@yahoo.com
DeleteCan it be used to swithch view according to the selected list item?
ReplyDeleteyet i havnt try but it may be possible using check value of selected item.
DeletePlease send me the full source code emmacodos@yahoo.com. Thanks
ReplyDeleteHi, I needed this code. Please send me source code huydungktv@gmail.com. Thanks
ReplyDeletegreat, please send me the full source code, Thanks!!!
ReplyDeleteduffz@gmx.li
Happy new 2015 :)
Hola que tal me has ayudado mucho con este tutorial, tenia que montar items distintos en un listview. Muchas gracias amigo. SALUDOS
ReplyDeleteHi, please send full source code janandjai@gmail.com . I have faced a issue related to classcastexception while scrolling listview
ReplyDeleteIt is indeed useful. Glad you tried this, we can follow your steps.
ReplyDeleteThanks. Alex
Please send me the full source code kang6264@gmail.com Thanks
ReplyDeleteplease send me source code kjmgganari@gmail.com
ReplyDeleteHello!!
ReplyDeletethank you for your help! World need more people like you. Could you send me the full code to rogerabino@gmail.com ?
you saved my life. thanks a lot
ReplyDeleteI cant understand this part:
ReplyDeleteif (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
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.
DeleteHe is checking if the item in that position belong to that type, then return the corresponding row type and inflate the correct layout.
Hi Please send me source code on neeraj.g.25612@gmail.com
ReplyDeleteCool, please send me the source code, Thanks
ReplyDeletemanu.badoni@gmail.com
This comment has been removed by the author.
ReplyDeleteKamaliya, 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
ReplyDeletePlease my i have the source code on lucianoblz@yahoo.com .Thank you
ReplyDeleteplease send m the source code,Thanks
ReplyDeletebhagyashrigaikwad1@gmail.com
Perfect
ReplyDeleteplease send source code on substar55@yahoo.com
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehi, please send me source code on m-randa@hotmail.fr
ReplyDeleteHi , Can you please send the source code on mbabhi92@gmail.com
ReplyDelete