首页 技术 正文
技术 2022年11月20日
0 收藏 308 点赞 3,482 浏览 3723 个字

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. 结果图:

Android进阶笔记11:ListView篇之ListView显示多种类型的条目(item)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898