I have ListView.
I have 3 sets of views:
- Imageview
- VideoView
- Textview.
I have a webserver which returns images, videos and texts. So based on the response from the server i would like to inflate the views for the row's in listview.
I am aware of inflating a custom layout using a custom adapter.
I would like to know how to inflate views types using a custom adapter?.
For example :
If the webserver returns image, inflate imageview.
Associate each webservice response with ID 0-for Images, 1-for Texts and 2-for Videos. Store ID in a ArrayList. Then Override getItemViewType() and getViewTypeCount(). Inflate appropriate views in getView().
@Override
public int getItemViewType(int position)
{
int type;
if (ID.get(position)== 0){
type = TYPE_ITEM1; //type 0 for image
} else if (ID.get(position) == 1){
type = TYPE_ITEM2; //type 1 for text
}else {
type = TYPE_ITEM3; //type 2 for videos
}
return type;
}
@Override
public int getViewTypeCount() {
return 3; //three different layouts to be inflated
}
来源:https://stackoverflow.com/questions/12337331/custom-listview-with-different-row-layouts-to-be-inflated-depending-on-webservic