Android进阶笔记10:ListView篇之ListView显示多种类型的条目(item)
ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似.
1. 这是MainActivity,MainActivity的布局就是一个ListView,太简单了这里就不写了,直接来到MainActivity,如下:
1 public class MainActivity extends Activity { 2 3 private ListView lv; 4 private List<People> lists; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 lv = (ListView) findViewById(R.id.lv); 11 12 lists=new ArrayList<>(); 13 //0代表学生,1代表老师 14 People people1 = new People(0, "10");//类型,钱 15 People people2 = new People(1, "100"); 16 People people3 = new People(1, "100"); 17 People people4 = new People(0, "10"); 18 lists.add(people1); 19 lists.add(people2); 20 lists.add(people3); 21 lists.add(people4); 22 23 lv.setAdapter(new MyAdapter()); 24 } 25 26 class MyAdapter extends BaseAdapter{ 27 28 @Override 29 public int getCount() { 30 return lists.size(); 31 } 32 33 @Override 34 public Object getItem(int i) { 35 return lists.get(i ); 36 } 37 38 @Override 39 public long getItemId(int i) { 40 return i; 41 } 42 43 @Override 44 public int getItemViewType(int position) { 45 if(lists.get(position).getType()==0){//当前JavaBean对象的类型 46 return 0;//学生类型 47 }else if(lists.get(position).getType()==1){ 48 return 1;//老师类型 49 }else { 50 return 100; 51 } 52 53 } 54 55 @Override 56 public int getViewTypeCount() { 57 return 2;//总共有两个类型 58 } 59 60 @Override 61 public View getView(int position, View convertView, ViewGroup viewGroup) { 62 int currentType = getItemViewType(position);//当前类型 63 if(currentType==0){//学生类型 64 StudentViewHolder studentViewHolder; 65 if(convertView==null){ 66 studentViewHolder=new StudentViewHolder(); 67 convertView=View.inflate(MainActivity.this,R.layout.item_lv_student,null); 68 studentViewHolder.tv0= (TextView) convertView.findViewById(R.id.num_money_stu); 69 convertView.setTag(studentViewHolder); 70 }else{ 71 studentViewHolder= (StudentViewHolder) convertView.getTag(); 72 } 73 //数据填充 74 studentViewHolder.tv0.setText(lists.get(position).getMoney()); 75 }else if(currentType==1){//老师类型 76 TeacherViewHolder teacherViewHolder; 77 if(convertView==null){ 78 teacherViewHolder=new TeacherViewHolder(); 79 convertView=View.inflate(MainActivity.this,R.layout.item_lv_teacher,null); 80 teacherViewHolder.tv1= (TextView) convertView.findViewById(R.id.num_money_teacher); 81 convertView.setTag(teacherViewHolder); 82 }else{ 83 teacherViewHolder= (TeacherViewHolder) convertView.getTag(); 84 } 85 //数据填充 86 teacherViewHolder.tv1.setText(lists.get(position).getMoney()); 87 } 88 89 90 return convertView; 91 92 } 93 } 94 /**学生item的Holder*/ 95 class StudentViewHolder { 96 TextView tv0; 97 } 98 /**老师item的Holder*/ 99 class TeacherViewHolder { 100 TextView tv1; 101 } 102 }
2. 上面使用到的bean类如下:
1 /** 2 * 区分 学生 和 老师 3 * type = 1 老师 4 * type = 0 学生 5 */ 6 public class People { 7 /**类型,0表示学生,1表示老师**/ 8 public int type; 9 public String money; 10 11 public People(int type, String money) { 12 this.type = type; 13 this.money = money; 14 } 15 16 public int getType() { 17 return type; 18 } 19 20 public void setType(int type) { 21 this.type = type; 22 } 23 24 public String getMoney() { 25 return money; 26 } 27 28 public void setMoney(String money) { 29 this.money = money; 30 } 31 }
3. ListView的Item的布局如下:
(1)学生Item布局,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 android:background="#00ff00" 7 > 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="学生的钱:" /> 13 14 <TextView 15 android:id="@+id/num_money_stu" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="10" /> 19 20 <Button 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:text="学生布局" 24 android:id="@+id/button" /> 25 26 </LinearLayout>
(2)老师item的布局,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 android:background="#f9a5b2"> 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="老师的钱:"/> 12 <TextView 13 android:id="@+id/num_money_teacher" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="100"/> 17 </LinearLayout>
4. 结果图: