标签:
ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似
这是MainActivity:,MainActivity的布局就是一个ListView
public class MainActivity extends Activity { private ListView lv; private List<People> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); lists=new ArrayList<>(); //0代表学生,1代表老师 People people1 = new People(0, "10");//类型,钱 People people2 = new People(1, "100"); People people3 = new People(1, "100"); People people4 = new People(0, "10"); lists.add(people1); lists.add(people2); lists.add(people3); lists.add(people4); lv.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return lists.size(); } @Override public Object getItem(int i) { return lists.get(i ); } @Override public long getItemId(int i) { return i; } @Override public int getItemViewType(int position) { if(lists.get(position).getType()==0){//当前JavaBean对象的类型 return 0;//学生类型 }else if(lists.get(position).getType()==1){ return 1;//老师类型 }else { return 100; } } @Override public int getViewTypeCount() { return 2;//总共有两个类型 } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { int currentType = getItemViewType(position);//当前类型 if(currentType==0){//学生类型 StudentViewHolder studentViewHolder; if(convertView==null){ studentViewHolder=new StudentViewHolder(); convertView=View.inflate(MainActivity.this,R.layout.item_lv_student,null); studentViewHolder.tv0= (TextView) convertView.findViewById(R.id.num_money_stu); convertView.setTag(studentViewHolder); }else{ studentViewHolder= (StudentViewHolder) convertView.getTag(); } //数据填充 studentViewHolder.tv0.setText(lists.get(position).getMoney()); }else if(currentType==1){//老师类型 TeacherViewHolder teacherViewHolder; if(convertView==null){ teacherViewHolder=new TeacherViewHolder(); convertView=View.inflate(MainActivity.this,R.layout.item_lv_teacher,null); teacherViewHolder.tv1= (TextView) convertView.findViewById(R.id.num_money_teacher); convertView.setTag(teacherViewHolder); }else{ teacherViewHolder= (TeacherViewHolder) convertView.getTag(); } //数据填充 teacherViewHolder.tv1.setText(lists.get(position).getMoney()); } return convertView; } } /**学生item的Holder*/ class StudentViewHolder { TextView tv0; } /**老师item的Holder*/ class TeacherViewHolder { TextView tv1; } }
JavaBean对象:
/**模仿的Json对象,Json对象里一定要带类型,区分学生和老师 * Created by xhj on 16-1-8. */ public class People { /**类型,0表示学生,1表示老师*/ public int type; public String money; public People(int type, String money) { this.type = type; this.money = money; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } }
ListView学生Item的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#00ff00" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学生的钱:" /> <TextView android:id="@+id/num_money_stu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="学生布局" android:id="@+id/button" /> </LinearLayout>
ListView老师Item的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#f9a5b2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老师的钱:"/> <TextView android:id="@+id/num_money_teacher" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="100"/> </LinearLayout>
结果图:
标签:
原文地址:http://www.cnblogs.com/android-yus/p/5112816.html