ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似.

1. 这是MainActivity,MainActivity的布局就是一个ListView,太简单了这里就不写了,直接来到MainActivity,如下:

 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;
}
}

2. 上面使用到的bean类如下:

 /**
* 区分 学生 和 老师
* type = 1 老师
* type = 0 学生
*/
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;
}
}

3. ListView的Item的布局如下:

(1)学生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>

(2)老师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>

4. 结果图:

最新文章

  1. Sublime Text3 快捷键
  2. php随机生成验证码
  3. [Android Pro] fragment中嵌套viewpager,vierpager中有多个fragment,不显示
  4. [android警告] AndroidManifest.xml警告 Should explicitly set android:allowBackup to true or false
  5. asp.net 获取客户机IP地址
  6. 玩转EasyUi弹出框
  7. Java基于Servlet过虑器
  8. log file sync
  9. 【转载】如何在C语言中调用shell命令
  10. javascript进击(八)JSON
  11. AVD启动不了 ANDROID_SDK_HOME is defined but could not find *.ini
  12. (1) 类构造块,this(),static,单例模式串讲
  13. 201521123113《Java程序设计》第10周学习总结
  14. 我的摸索过程之IIS下配置asp.net 的注意事项
  15. supermap中预览osgb格式的倾斜摄影文件
  16. python之路——19
  17. c# 静态构造函数与构造函数的调用先后
  18. jmeter之结果重定向
  19. vue 数据管道
  20. AJPFX技术分析入门

热门文章

  1. mybatis系列-07-输出映射
  2. [Hive - LanguageManual] VirtualColumns
  3. Classes and Objects :类和对象(1)
  4. linux下无法安装VMware的解决方法
  5. I - Control - HDU 4289 (最大流)
  6. osx升级到10.10后,用pod install报错最终解决办法
  7. Umbraco Forms 中的Recaptcha遇到的问题
  8. C++获取系统的Mac地址
  9. UVaLive 7374 Racing Gems (DP,LIS)
  10. Failed to execute query: Duplicate entry &#39;0&#39; for key &#39;PRIMARY&#39;