首页 > 代码库 > 自定义ListView
自定义ListView
一.定义内容类Fruit
public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId) {
this.imageId = imageId;
this.name = name;
}
public int getImageId() {
return imageId;
}
public String getName() {
return name;
}
}
二.定义item布局,fruit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_image"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_name"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
/>
</LinearLayout>
三.定义Adapter,FruitAdpater
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
this.resourceId = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView imageView = (ImageView)view.findViewById(R.id.fruit_image);
TextView textView = (TextView)view.findViewById(R.id.fruit_name);
imageView.setImageResource(fruit.getImageId());
textView.setText(fruit.getName());
return view;
}
}
FruitAdapter 重写了父类的一组构造函数,用于将上下文、ListView 子项布局的id
和数据都传递进来。另外又重写了getView()方法,这个方法在每个子项被滚动到屏幕内的
时候会被调用。在getView 方法中,首先通过getItem()方法得到当前项的Fruit 实例,然
后使用LayoutInflater 来为这个子项加载我们传入的布局, 接着调用View 的
findViewById()方法分别获取到ImageView 和TextView 的实例,并分别调用它们的
setImageResource()和setText()方法来设置显示的图片和文字,最后将布局返回,这样
我们自定义的适配器就完成了。
四.Activity调用
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruit();
FruitAdapter fruitAdapter = new FruitAdapter(this, R.layout.fruit_item, fruitList);
ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(fruitAdapter);
}
private void initFruit(){
Fruit fruit;
fruit= new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
fruit = new Fruit("苹果", R.mipmap.ic_launcher);
fruitList.add(fruit);
}
五.最终成图
自定义ListView