ListView:展示大量数据,可以将屏幕外的数据划入屏幕内,主要任:将数据填充到布局,处理用户的选择点击等操作。
- 添加ListView控件
- 添加适配器
- 将适配器添加到ListView
添加控件
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
添加适配器(第一个参数获得上下文,第二个参数添加一个简单的子项布局到适配器中,展示的数据)
ArrayAdapter<String> fruit = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,data);
将适配器添加到ListView
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(fruit);
以这种方式构造一种定制的ListView界面
set、get方法
package com.example.bbw.listviewdemo;
/**
* Created by bbw on 2017/7/20.
*/
public class Fruit {
private int imageId;
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public Fruit( String name,int imageId) {
this.imageId = imageId;
this.name = name;
}
}
子项布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fruit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="10sp" />
<ImageView
android:id="@+id/fruit_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
自定义适配器:
package com.example.bbw.listviewdemo;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by bbw on 2017/7/20.
* 新的适配器
*/
public class FruitAdapter extends ArrayAdapter<Fruit>{
private ImageView fruit_image;
private TextView fruit_name;
private int resourceId;
public FruitAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Fruit> objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
//重写getView,返回的view作为listView的子项布局
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
if (convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);//为子项加载传入的布局
}else{
view = convertView;
}
Fruit fruit = getItem(position);//获得当前位置的fruit对象
fruit_image = view.findViewById(R.id.fruit_image);
fruit_name = view.findViewById(R.id.fruit_name);
fruit_image.setImageResource(fruit.getImageId());
fruit_name.setText(fruit.getName());
return view;
}
}
初始化数据
private void initFruit() {
for (int i=0;i<2;i++){
Fruit apple = new Fruit("apple",R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("banana",R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("orange",R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("pear",R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("grape",R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("pineapple",R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("strawberry",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("cherry",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("mango",R.drawable.mango_pic);
fruitList.add(mango);
}
添加适配器
FruitAdapter fruit = new FruitAdapter(MainActivity.this,R.layout.fruit,fruitList);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(fruit);