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>
結果圖: