SoFunction
Updated on 2025-03-01

Detailed explanation of different view layouts and example codes for displaying in Android Listview

Show different view layouts in Android Listview

1. Use scenarios

When overriding the BaseAdapter of a ListView, we often multiplex convertView in the getView() method to improve performance. When the convertView is a single layout of the same type, it can be recycled and reused. However, when multiple Item layout types are different, there will be problems with the recycling and reuse of convertView. For example, some behaviors are pure text, while some lines are mixed with pictures and text. Here, the pure text behavior is a kind of layout, and the second type of layout is a kind of layout. A single type of ListView is very simple. The following is a case where ListView contains multiple types of view layouts.

Layouts containing different Items

We need to do these tasks:

1) Rewrite getViewTypeCount() – How many different layouts does this method return
2) Rewrite getItemViewType(int) – Return the corresponding Item according to position
3) Create the correct convertView in getView according to the type of view item

3. Case

import ; 

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 

public class listViewTest extends Activity { 
/** Called when the activity is first created. */
ListView listView; 
MyAdapter listAdapter; 
ArrayList<String> listString; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
(savedInstanceState); 
setContentView(); 
listView = (ListView)(); 
listString = new ArrayList<String>(); 
for(int i = 0 ; i < 100 ; i++) 
{ 
((i)); 
} 
listAdapter = new MyAdapter(this); 
(listAdapter); 
} 

class MyAdapter extends BaseAdapter{ 
Context mContext; 
LinearLayout linearLayout = null; 
LayoutInflater inflater; 
TextView tex; 
final int VIEW_TYPE = 3; 
final int TYPE_1 = 0; 
final int TYPE_2 = 1; 
final int TYPE_3 = 2; 

public MyAdapter(Context context) { 
// TODO Auto-generated constructor stub 
mContext = context; 
inflater = (mContext); 
} 

@Override 
public int getCount() { 
// TODO Auto-generated method stub 
return (); 
} 

//Each convert view will call this method to get the currently required view style@Override 
public int getItemViewType(int position) { 
// TODO Auto-generated method stub 
int p = position%6; 
if(p == 0) 
return TYPE_1; 
else if(p < 3) 
return TYPE_2; 
else if(p < 6) 
return TYPE_3; 
else
return TYPE_1; 
} 

@Override 
public int getViewTypeCount() { 
// TODO Auto-generated method stub 
return 3; 
} 

@Override 
public Object getItem(int arg0) { 
// TODO Auto-generated method stub 
return (arg0); 
} 

@Override 
public long getItemId(int position) { 
// TODO Auto-generated method stub 
return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
viewHolder1 holder1 = null; 
viewHolder2 holder2 = null; 
viewHolder3 holder3 = null; 
int type = getItemViewType(position); 

//No convertView, each control needs to be newif(convertView == null) 
{ 
("convertView = ", " NULL"); 

//Click the layout of new according to the current required styleswitch(type) 
{ 
case TYPE_1: 
convertView = (.listitem1, parent, false); 
holder1 = new viewHolder1(); 
 = (TextView)(.textview1); 
 = (CheckBox)(); 
("convertView = ", "NULL TYPE_1"); 
(holder1); 
break; 
case TYPE_2: 
convertView = (.listitem2, parent, false); 
holder2 = new viewHolder2(); 
 = (TextView)(.textview2); 
("convertView = ", "NULL TYPE_2"); 
(holder2); 
break; 
case TYPE_3: 
convertView = (.listitem3, parent, false); 
holder3 = new viewHolder3(); 
 = (TextView)(.textview3); 
 = (ImageView)(); 
("convertView = ", "NULL TYPE_3"); 
(holder3); 
break; 
} 
}else{ 
//There is a convertView, and the unused layout is obtained by style.switch(type) 
{ 
case TYPE_1: 
holder1 = (viewHolder1) (); 
("convertView !!!!!!= ", "NULL TYPE_1"); 
break; 
case TYPE_2: 
holder2 = (viewHolder2) (); 
("convertView !!!!!!= ", "NULL TYPE_2"); 
break; 
case TYPE_3: 
holder3 = (viewHolder3) (); 
("convertView !!!!!!= ", "NULL TYPE_3"); 
break; 
} 
} 

//Set resourcesswitch(type) 
{ 
case TYPE_1: 
((position)); 
(true); 
break; 
case TYPE_2: 
((position)); 
break; 
case TYPE_3: 
((position)); 
(); 
break; 
} 

return convertView; 
} 
} 

//Control resources for each layoutclass viewHolder1{ 
CheckBox checkBox; 
TextView textView; 
} 

class viewHolder2{ 
TextView textView; 
} 

class viewHolder3{ 
ImageView imageView; 
TextView textView; 
} 
}

Thank you for reading, I hope it can help you. Thank you for your support for this site!