Reusing Views in Android Listview with 2 Different Layouts

Android: reusing a customadapter when dealing with multiple layouts

If row.xml and row_alt.xml only have two different widget, I suggest that you create two different Adapter. If really there is a common behavior between this two Adapters, create a parent class that will implements the common behavior and make your two adapter extends this parent class. Then each Adapter implements its own [getView()](https://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)) method.

Then for further optimizations use the ViewHolder Pattern.

I also suggest that you take a look at RecyclerView which is the new version of ListView and don't need the ViewHolder Pattern

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.

Using two different layouts for child items in ExpandableListView

I'm pretty sure that you are not returning the good child values when convertView != null on your getChildView method.
Try to set your values after your condition, like that :

TextView textView = null;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
String myText = childData.get(groupPosition).get(childPosition).get("chapter");

switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
break;
}
}

textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));

If you still have a problem with that, please put a comment on my answer to tell me and I will see what I can do.

Recycling ListView rows with different row layouts

Always recycle rows! Your implementation of getItemViewType (and getViewTypeCount) instructs the ListView when to recycle what types of views so that they won't get mixed up.

Your code snippet looks good. So, it all should work. The only slightly odd part is the if(type == TYPE_3). Did you mean if(type == TYPE_2)?

The question is: Do you have problem/exception in your code right now?

Recycling views in a listview, worth it?

A couple of reasons to recycle views:

  • Object creation is relatively expensive. Every additional object that is created needs to be dealt with by the garbage collection system, and at least temporarily increases your memory footprint
  • This is more important for more complex views, but inflating and laying out the view objects can be expensive. Most often, you are only making minor changes to the view in getView that won't affect the layout (e.g, setting text) so you might be able to avoid the layout overhead.
  • Remember that Android is designed to be run in a resource constrained environment.
  • Finally, its already done for you, and it certianly doesn't hurt anything, so why not use it?


Related Topics



Leave a reply



Submit