新建ListViewTest项目
//activity_main.xml<?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\"><ListViewandroid:id=\"@+id/list_view\"android:layout_width=\"match_parent\"android:layout_height=\"match_parent\"/></LinearLayout>
//MainActivity//数组中的数据是无法直接传递给ListView的//需要借助适配器来完成//ArrayAdapter可以通过泛型来指定需要适配的数据类型//然后在构造函数中把要适配的数据传入//android.R.layout.simple_list_item_1作为子项布局的id//这是一个Android内置的布局文件,里面只有一个TextView,可以简单地显示一段文本public class MainActivity extends AppCompatActivity {private String[] data ={ \"Apple\", \"Banana\", \"Orange\", \"Watermelon\", \"Pear\",\"Grape\", \"Pineapple\", \"Strawberry\",\"Cherry\",\"Mango\",\"Apple\", \"Banana\", \"Orange\", \"Watermelon\", \"Pear\",\"Grape\", \"Pineapple\", \"Strawberry\",\"Cherry\",\"Mango\"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);ListView listView = (ListView) findViewById(R.id.list_view);listView.setAdapter(adapter);}
定制ListView界面
//新建类Fruit.java//name表示水果名//imageId表示水果对应图片的资源idpackage 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子项指定一个自定义布局//新建布局fruit_item.xml<?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=\"wrap_content\"><ImageViewandroid:id=\"@+id/fruit_image\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"/><TextViewandroid:id=\"@+id/fruit_name\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:layout_gravity=\"center_vertical\"android:layout_marginLeft=\"10dp\"/></LinearLayout>
//创建一个自定义的适配器//这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类//新建类FruitAdapter.javapublic class FruitAdapter extends ArrayAdapter<Fruit> {private int resourceId;public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){super(context, textViewResourceId, objects);resourceId = textViewResourceId;}@Overridepublic View getView(int position, View convertView, ViewGroup parent){Fruit fruit = getItem(position); //获取当前项的Fruit实例View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);ImageView fruitImage = (ImageView) findViewById(R.id.fruit_image);TextView fruitName = (TextView) findViewById(R.id.fruit_name);fruitImage.setImageResource(fruit.getImageId());fruitName.setText(fruit.getName());return view;}}
//MainActivitypublic class MainActivity extends AppCompatActivity {// private String[] data =// { \"Apple\", \"Banana\", \"Orange\", \"Watermelon\", \"Pear\",// \"Grape\", \"Pineapple\", \"Strawberry\",\"Cherry\",\"Mango\",// \"Apple\", \"Banana\", \"Orange\", \"Watermelon\", \"Pear\",// \"Grape\", \"Pineapple\", \"Strawberry\",\"Cherry\",\"Mango\"};private List<Fruit> fruitList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initFruits();FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);ListView listView = (ListView) findViewById(R.id.list_view);listView.setAdapter(adapter);}//初始化所有的水果数据private void initFruits(){for(int i = 0; i < 2; i++){Fruit apple = new Fruit(\"Apple\",R.drawable.apple);fruitList.add(apple);Fruit banana = new Fruit(\"Babnana\",R.drawable.banana);fruitList.add(banana);Fruit orange = new Fruit(\"Orange\",R.drawable.orange);fruitList.add(orange);Fruit watermelon = new Fruit(\"Watermelon\",R.drawable.watermelon);fruitList.add(watermelon);Fruit pear = new Fruit(\"Pear\",R.drawable.pear);fruitList.add(pear);Fruit grape = new Fruit(\"Grape\",R.drawable.grape);fruitList.add(grape);Fruit pineapple = new Fruit(\"Pineapple\",R.drawable.pineapple);fruitList.add(pineapple);Fruit strawberry = new Fruit(\"Strawberry\",R.drawable.strawberry);fruitList.add(strawberry);Fruit cherry = new Fruit(\"Cherry\",R.drawable.cherry);fruitList.add(cherry);Fruit mango = new Fruit(\"Mango\",R.drawable.mango);fruitList.add(mango);}}}
只要修改fruit_item.xml,就可以定制出各种复杂界面了。
提升ListView运行效率
//修改FruitAdapter//getView()方法每次都将布局重新加载了一遍//convertView参数就是用于将之前加载好的布局进行缓存,以便之后可以重用@Overridepublic View getView(int position, View convertView, ViewGroup parent){Fruit fruit = getItem(position); //获取当前项的Fruit实例View view;if(convertView == null){view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);}else{view = convertView;}//View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);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//但每次还是会调用View的findViewById来获取一次控件实例//可以借助一个ViewHolder来优化class ViewHolder{ImageView fruitImage;TextView fruitName;}@Overridepublic View getView(int position, View convertView, ViewGroup parent){Fruit fruit = getItem(position); //获取当前项的Fruit实例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存储在View中}else{view = convertView;viewHolder = (ViewHolder) view.getTag(); //重新获取ViewHolder}//View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);//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());viewHolder.fruitImage.setImageResource(fruit.getImageId());viewHolder.fruitName.setText(fruit.getName());return view;}}
ListView点击事件
//修改MainActivity@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initFruits();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() {@Overridepublic 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();}});}