3.5最常用和最难用的控件----ListView
ListView非常常见,也是Android中最常见的空间之一,几乎所有程序都会用到,QQ聊天,刷微博,诸如此类。
ListView用法也是比较复杂。下面以一个小项目来练习ListView。
新建一个ListViewTest项目,默认设置。
public class MainActivity extends AppCompatActivity {
private String[] data = {"Apple","Banana","Orange","Watermelon",
"Pear","Grape","Pineapple","Strawberry","Cherry","Mango",
"Apple","Banana","Orange","Pineapple","Strawberry","Cherry","Mango",
"Apple","Banana","Orange"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter(
MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
如图所示,先提供好数据,String类型的数组数据,数组中的数据无法直接传递给ListView的,要借助适配器来完成。这里用ArrayAdapter。它可通过泛型来指定要适配的实现类,然后在构造函数中把适配的数据传入。
ArrayAdapter有多个构造函数重载,这里把ArrayAdapter泛型指定为String,再在ArrayAdapter构造函数中依次传入当前上下文、ListView子项布局的id、以及要适配的数据。使用了android.R.layout.simple_list_item_1作为ListView的子项布局id,这是Android内部的布局文件,里面只有一个TextView,可用于简单显示一段文本。这样适配器对象就构建好了。
3.5.2定制ListView界面
准备好图片,对应上面提供的每一种水果。让水果名称的旁边都有一个图样。
定义一个实体类,作为List适配器的适配类型。新建Fruit类,代码如下:
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
然后为了ListView的子项指定一个自定义的布局,新建fruit_item.xml,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
LinearLayout>
创建一个自定义适配器,继承自ArrayAdapter,将泛型指定为Fruit类。新建类FruitAdapter。
public class FruitAdapter extends ArrayAdapter {
private int resourcedId;
public FruitAdapter(Context context, int textViewResourceId, List objects) {
super(context,textViewResourceId,objects);
resourcedId = textViewResourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourcedId,parent,
false);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
修改MainActivity代码:
public class MainActivity extends AppCompatActivity {
private List fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this,
R.layout.fruit_item,fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0;i<2;i++) {
Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Apple",R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Apple",R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Apple",R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Apple",R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("Apple",R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Apple",R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Apple",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Apple",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Apple",R.drawable.mango_pic);
fruitList.add(mango);
}
}
}