1.定義布局與實(shí)體類
①創(chuàng)建好要顯示數(shù)據(jù)的實(shí)體類
package com.example.listviewtest;
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子項(xiàng)的布局
2.自定義適配器
①繼承于ArrayAdapter<實(shí)體類>
public class FruitAdapter extends ArrayAdapter
? ? ? ②重寫構(gòu)造函數(shù)
public FruitAdapter(Context context, int textViewResourceId, List objects)?
? ? ? ?③重寫getView()方法
@Override
public View getView(int position, View convertView, ViewGroup parent)
? ? ? ? ? ? ? ? ? ? ④提高性能:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?a.利用convertView 緩存加載好的布局
? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? b.利用viewHolder 緩存已經(jīng)獲取的控件
package com.example.listviewtest;import android.content.Context;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;
public class FruitAdapter extends ArrayAdapter{? ??
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); // 獲取當(dāng)前項(xiàng)的Fruit實(shí)例
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById (R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById (R.id.fruit_name);
view.setTag(viewHolder); // 將ViewHolder存儲(chǔ)在View中
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag(); // 重新獲取ViewHolder
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder {
ImageView fruitImage;
TextView fruitName;
}
}
3.修改mainActivity代碼
①創(chuàng)建適配器
參數(shù)為(上下文,自定義布局文件, 泛型數(shù)據(jù))
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
②將適配器傳遞給listView?
ListView listView = (ListView) findViewById(R.id.list_view);
?listView.setAdapter(adapter);
③添加按鈕事件監(jiān)聽
使用匿名內(nèi)部類作為事件監(jiān)聽器類方式實(shí)現(xiàn)事件監(jiān)聽
listView.setOnItemClickListener(new AdapterView.OnItemClickListener())......
完整代碼
package com.example.listviewtest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {? ? private ListfruitList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); // 初始化水果數(shù)據(jù)
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
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 = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
}
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("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);
}
}
}
2.