Android-ListView

常用属性

  1. listSelector: 当条目被点击后,改变条目的背景颜色
  2. divider: 设置分割线的颜色
  3. dividerHeight: 设置分割线的高度
  4. srcollbars: 是否显示滚动条
  5. fadingEdge: 去掉上下黑色阴影

简单的ListView

资源文件下载

创建布局(activity_main.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#336633"
android:gravity="center_vertical"
android:layout_height="80dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/listTitle"
android:layout_marginLeft="20dp"
android:textColor="@color/white"
android:textStyle="bold"
android:text="TITLE"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="match_parent"/>
</LinearLayout>

创建列表项布局(item.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="100dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#339933"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 任意一张图片做默认图片即可-->
<ImageView
android:layout_width="80dp"
android:layout_marginLeft="10dp"
android:src="@mipmap/moren"
android:id="@+id/foodIcon"
android:layout_height="80dp"/>
<TextView
android:text="foodName"
android:layout_marginLeft="20dp"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

创建MyListTest类,该类继承自BaseAdapter,ListView的适配器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
private String[] foodName;
private int[] foodIcon;
private Context context;
public MyAdapter(String[] foodName,int[] foodIcon,Context context){
this.context = context;
this.foodIcon = foodIcon;
this.foodName = foodName;
}
@Override
// 返回列表的总列数
public int getCount() {
return foodIcon.length;
}

@Override
// 返回Item的数据对象
public Object getItem(int position) {
return null;
}

@Override
// //返回item的id
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 如果没有拿到过ite资源文件
if (convertView == null){
convertView = View.inflate(context,R.layout.item,null);
}
// 从item中找到要设置的子控件
TextView textView = convertView.findViewById(R.id.foodName);
ImageView imageView = convertView.findViewById(R.id.foodIcon);
// 设置
textView.setText(foodName[position]);
imageView.setImageResource(foodIcon[position]);
return convertView;
}
}

在MainActivity中对ListView进行应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private int[] foodIcon = new int[]{
R.mipmap.dangao,R.mipmap.cha,R.mipmap.hanbao,R.mipmap.jitui,R.mipmap.kafei,R.mipmap.kaixinguo,R.mipmap.lanmei,
R.mipmap.ningmeng,R.mipmap.niupai,R.mipmap.pijiu,R.mipmap.pisa,R.mipmap.qiyiguo,R.mipmap.regou,R.mipmap.sanmingzhi,
R.mipmap.shengnvguo,R.mipmap.shutiao,R.mipmap.tusi,R.mipmap.yimian,R.mipmap.yingtao,R.mipmap.zhangyuxiaowanzi
};
private String[] foodName = new String[]{
"蛋糕","茶","汉堡","鸡腿","咖啡","开心果","蓝莓","柠檬","牛排","啤酒","披萨","奇异果","热狗","三明治","圣女果","薯条","吐司","意面","樱桃","章鱼小丸子"
};
private ListView listView;
private TextView listTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = findViewById(R.id.listView);
listTitle = findViewById(R.id.listTitle);
MyAdapter myAdapter = new MyAdapter(foodName,foodIcon,this);
listView.setAdapter(myAdapter);
}
}

ListView的item添加点击事件

在MainActivity中插入

1
2
3
4
5
6
7
//        给item添加事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listTitle.setText(foodName[position]);
}
});

ListView的优化

优化原因:当滑动列表时,划出屏幕的item就会被摧毁掉,新出现的item就会创建一个新的view对象,并且findViewById也十分耗时

新知识点:setTag()是设置标签,getTag()是获取标签,经常用来存储一些view的数据(大概相当于js中setAttribute给标签添加属性)

在MyAdapter类中创建一个内部类

1
2
3
4
5
6
final class ViewHolder{
// 将item下需要调用的子控件全部填入
TextView textView;
ImageView imageView;
}

在MyListTest类的getView方法中使用ViewHolder类创建对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    @Override
public View getView(int position, View convertView, ViewGroup parent) {
// 声明viewHolder变量
ViewHolder viewHolder;
if (convertView == null){
// 创建ViewHolder对象
viewHolder = new ViewHolder();
convertView = View.inflate(context,R.layout.item,null);
// 将获取到的view赋值给viewHolder类的变量
viewHolder.textView = convertView.findViewById(R.id.text1);
viewHolder.imageView = convertView.findViewById(R.id.img1);
// 将viewHolder附加给convertView
convertView.setTag(viewHolder);
}
else{
// 从convertView中取出viewHolder
viewHolder = (ViewHolder) convertView.getTag();
}
// 得到ViewHolder类变量的值
viewHolder.textView.setText(iconName[position]);
viewHolder.imageView.setImageResource(iconId[position]);
return convertView;
}