步骤:
一、xml 自定义listview中包含的控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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>
二、定义一个实体类,作为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 int getImageID() { return imageID; } }
三、自定义适配器,需要继承于ArrayAdapter,并将泛型指定为Fruit类,新建FruitAdapter
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceID; public FruitAdapter(Context context, int textViewResourceID, List<Fruit> objects) { super(context, textViewResourceID, objects); resourceID = textViewResourceID; } @Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); // 获取当前项的Fruit实例 // 普通创建:滚动时会每次都重新加载一遍 // View view = LayoutInflater.from(getContext()).inflate(resourceID, parent, false); // listview优化创建 /** * getView()方法中的的convertView参数,这个参数用于将之前的加载好的布局进行缓存,以便以后复用 */ /* View view; if (convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceID, parent, false); }else { view = convertView; } 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; */ // 控件优化创建 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); // 将ViewHodler 存储在View中 view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.fruitImage.setImageResource(fruit.getImageID()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder { ImageView fruitImage; TextView fruitName; } }
四、在需要的页面添加
public class MainActivity extends AppCompatActivity { // 测试数据2 定制listView private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 自定义ListView的界面 // 初始化水果数据 initFruits(); FruitAdapter adapterF = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapterF); } private void initFruits() { for (int i = 0; i<2; i++) { Fruit apple = new Fruit("Apple", R.drawable.apple_pic); fruitList.add(apple); } } }