Android: Xml Layout for a Listview with Different Items

Android: xml layout for a listview with different items

You need to override getViewItemType and getViewTypeCount. You will also need to have custom layouts.

getItemViewType(int position) - returns information which layout type you should use based on position.

You should have a look at the video in the link.

http://www.youtube.com/watch?v=wDBM6wVEO70

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;

Then

int type;
@Override
public int getItemViewType(int position) {

if (position== 0){
type = TYPE_ITEM1;
} else if (position == 1){
type = TYPE_ITEM2;
}
else
{
type= TYPE_ITEM3 ;
}
return type;
}

@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
// instead of if else you can use a case
if (row == null) {
if (type == FIRST_TYPE) {
//infalte layout of type1
}
if (type == SECOND_TYPE) {
//infalte layout of type2
} else {
//infalte layout of normaltype
}
}

Android ListView with different layouts for each row

Since you know how many types of layout you would have - it's possible to use those methods.

getViewTypeCount() - this methods returns information how many types of rows do you have in your list

getItemViewType(int position) - returns information which layout type you should use based on position

Then you inflate layout only if it's null and determine type using getItemViewType.

Look at this tutorial for further information.

To achieve some optimizations in structure that you've described in comment I would suggest:

  • Storing views in object called ViewHolder. It would increase speed because you won't have to call findViewById() every time in getView method. See List14 in API demos.
  • Create one generic layout that will conform all combinations of properties and hide some elements if current position doesn't have it.

I hope that will help you. If you could provide some XML stub with your data structure and information how exactly you want to map it into row, I would be able to give you more precise advise. By pixel.

Android how to inflate ListView with two different items

There are a few different scenarios that you will see when getView() is called:

  1. convertView is null: In this case just create the layout that you need depending upon noImg;

  2. convertView is defined but is of the wrong type. In this case, you will need to discard the old view and create the one you need;

  3. convertView is defined but is of the correct type, in which case, we do nothing with it.

The following code is one approach to making sure that you are always working with the right layout and can find the ids that you expect. This code uses a tag in the inflated layout to identify the layout type that can be checked.

if (convertView == null || noImg != (boolean) convertView.getTag()) {
// Either there is no view defined or it is the wrong type. Create
// the type we need.
if (noImg) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item_no_img, parent, false);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item, parent, false);
}
convertView.setTag(noImg);
}

Android ListView Item layout variations

The duplicate post mentioned by Sandi did what I wanted very easily, and without any duplication of XML files or code to rearrange order. Thanks to everyone that responded and got me here. I just wanted to make sure that I posted what easily worked for me.

I have a TAB with two different ListViews each with their own ListItem XML layouts, and this reversed the order perfectly in the onCreateView for the TabFragment.java in the main Layout and in each of the ListItem layouts!

    // Inflate this Tab's checklist XML
root = inflater.inflate(R.layout.tab_fragment, container, false);
if (leftHanded) {
LinearLayout linearLayout = root.findViewById(R.id.tabLinearLayout);
linearLayout.setLayoutDirection(LinearLayout.LAYOUT_DIRECTION_RTL);
}

android listview different views for every item

You will need a generally more complicated Adapter. In particular, you need to override getViewTypeCount() and getItemViewType(). You can take a look at my SackOfViewsAdapter as a way of doing this for short lists, or my MergeAdapter for blending individual views with the results of database or Web service queries.



Related Topics



Leave a reply



Submit