ListView是android中最常用的控件之一,几乎所有的应用程序都会用它. ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据会滚动出屏幕外,比如手机联系人、微博等.
定义实体类Fruit作为ListView的适配器类型。
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 void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
然后为ListView的子项新建一个我们自定义的布局,在layout下新建fruit_item.xml文件,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.l.MainActivity">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
上述布局中ImageView用于显示水果的图片,TextView用于显示水果的名称。
接下来创建自定义的适配器,这个适配器FruitAdapter继承自ArrayAdapter,并将泛型指定为我们定义的Fruit类,如下:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List objects){
super(context,textViewResourceId,objects);
resourceId =textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
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;
}
}
FruitAdapter重写了父类的构造方法,又重写了getView方法,该方法在每个子项被滚动到屏幕内的时候会被调用,该方法中首先通过getItem方法得到当前的Fruit实例,然后使用LayoutInflater来为这个子项加载我们传入的布局,然后就是获得ImageView和TextView的实例,分别用于显示水果图片和名称。
然后就是MainActivity的修改,如下:
public class MainActivity extends AppCompatActivity {
private List fruits = 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,fruits);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initfruits(){
Fruit apple = new Fruit("苹果",R.mipmap.ic_launcher);
fruits.add(apple);
Fruit banana = new Fruit("香蕉",R.mipmap.ic_launcher);
fruits.add(banana);
Fruit taozi = new Fruit("桃子",R.mipmap.ic_launcher);
fruits.add(taozi);
Fruit putao = new Fruit("葡萄",R.mipmap.ic_launcher);
fruits.add(putao);
Fruit chengzi = new Fruit("橙子",R.mipmap.ic_launcher);
fruits.add(chengzi);
Fruit boluo = new Fruit("菠萝",R.mipmap.ic_launcher);
fruits.add(boluo);
Fruit pingguo = new Fruit("苹果",R.mipmap.ic_launcher);
fruits.add(pingguo);
Fruit caomei = new Fruit("草莓",R.mipmap.ic_launcher);
fruits.add(caomei);
}
}
这里添加的initFruits方法用于初始化水果数据。最后运行效果如下:
ListView如何响应用户的点击事件?
public class MainActivity extends AppCompatActivity {
private List fruits = 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,fruits);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Fruit fruit = fruits.get(position);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
}
private void initfruits(){
Fruit apple = new Fruit("苹果",R.mipmap.ic_launcher);
fruits.add(apple);
Fruit banana = new Fruit("香蕉",R.mipmap.ic_launcher);
fruits.add(banana);
Fruit taozi = new Fruit("桃子",R.mipmap.ic_launcher);
fruits.add(taozi);
Fruit putao = new Fruit("葡萄",R.mipmap.ic_launcher);
fruits.add(putao);
Fruit chengzi = new Fruit("橙子",R.mipmap.ic_launcher);
fruits.add(chengzi);
Fruit boluo = new Fruit("菠萝",R.mipmap.ic_launcher);
fruits.add(boluo);
Fruit pingguo = new Fruit("苹果",R.mipmap.ic_launcher);
fruits.add(pingguo);
Fruit caomei = new Fruit("草莓",R.mipmap.ic_launcher);
fruits.add(caomei);
}
}
这里使用了setOnItemClickListener方法为ListView注册了一个监听器,用户点击了ListView的任何一项时就会调用onItemClick方法在该方法中可以通过position参数来判断是点击了哪一项。