首页 > 移动平台 > 详细

《Android第一行代码》学习记录010 - ListView用法2

时间:2015-10-22 20:54:12      阅读:345      评论:0      收藏:0      [点我收藏+]

一、比起上次ListView的简单例子,这个例子相对要复杂,首先上图

技术分享

每一个列表项由两部分组成:一个ImageView,一个TextView。首先为列表项生成一个类:

 1 package com.matclone.ListViewTest;
 2 
 3 public class Country {
 4     private int imageId;
 5     private String name;
 6 
 7     public Country(int imageId, String name) {
 8         super();
 9         this.imageId = imageId;
10         this.name = name;
11     }
12 
13     public int getImageId() {
14         return imageId;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21 }

二、上例已经了解了构建一个ListView需要的条件有

  1. 数据来源:可以从数据库中读取、网络抓取等,属于MVC中的M层
  2. ListView中子项的布局文件,属于MVC中的V层
  3. 适配器:用于连接数据来源和布局,属于MVC中的C层

首先解决数据来源问题,每一个子项的图片都在res/drawable/文件夹中:

 1     private List<Country> countryList = new ArrayList<Country>();
 2 
 3     private void createList() {
 4         countryList.add(new Country(R.drawable.flag_belgium, "比利时"));
 5         countryList.add(new Country(R.drawable.flag_canada, "加拿大"));
 6         countryList.add(new Country(R.drawable.flag_europe, "欧盟"));
 7         countryList.add(new Country(R.drawable.flag_france, "法国"));
 8         countryList.add(new Country(R.drawable.flag_greece, "希腊"));
 9         countryList.add(new Country(R.drawable.flag_ireland, "爱尔兰"));
10         countryList.add(new Country(R.drawable.flag_italy, "意大利"));
11         countryList.add(new Country(R.drawable.flag_romania, "罗马利亚"));
12         countryList.add(new Country(R.drawable.flag_uk, "英国"));
13         countryList.add(new Country(R.drawable.flag_usa, "美国"));
14     }

 然后是ListView中子项的布局,也就是V层,新建布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5     
 6     <ImageView
 7         android:id="@+id/countryImage"
 8         android:layout_width="33dp"
 9         android:layout_height="33dp"
10         />
11     
12     <TextView
13         android:id="@+id/countryName"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_gravity="center_vertical"
17         android:layout_marginLeft="10dp"
18         />
19 
20 </LinearLayout>

接下来是适配器,即C层,需要做以下几件事:

  1. 由于我们是自己定义的Country类,所以我们需要自定义一个继承ArrayAdapter类的适配器
  2. 重写ArrayAdapter的getView()方法
  3. 在活动中使用继承的适配器,并为ListView的子项添加点击事件监听器
public class CountryAdapter extends ArrayAdapter<Country>

重写ArrayAdapter类的getView()方法,在这个方法中,我们需要做以下几件事:

  1. 通过ArrayAdapter的getItem()方法得到当前项的Country实例;
  2. 使用LayoutInflater加载ListView每一个子项的布局,也就是上面V层所定义的子项布局;
  3. 考虑到ListView快速滚动的性能,还需要定义一个ViewHolder来缓存每一个子项;
 1 package com.matclone.ListViewTest;
 2 
 3 import java.util.List;
 4 
 5 import android.content.Context;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.widget.ArrayAdapter;
10 import android.widget.ImageView;
11 import android.widget.TextView;
12 
13 public class CountryAdapter extends ArrayAdapter<Country> {
14     private int layoutFileId;
15 
16     public CountryAdapter(Context context, int textViewResourceId, List<Country> objects) {
17         super(context, textViewResourceId, objects);
18         // TODO Auto-generated constructor stub
19         this.layoutFileId = textViewResourceId;
20     }
21 
22     @Override
23     public View getView(int position, View convertView, ViewGroup parent) {
24         // TODO Auto-generated method stub
25         Country country = getItem(position);
26         View view;
27         ViewHolder viewHolder;
28         
29         if (convertView == null) {
30             view = LayoutInflater.from(getContext()).inflate(layoutFileId, null);
31             viewHolder = new ViewHolder();
32             
33             viewHolder.countryImage = (ImageView) view.findViewById(R.id.countryImage);
34             viewHolder.countryName = (TextView) view.findViewById(R.id.countryName);
35             view.setTag(viewHolder);
36         } else {
37             view = convertView;
38             viewHolder = (ViewHolder) view.getTag();
39         }
40         
41         viewHolder.countryImage.setImageResource(country.getImageId());
42         viewHolder.countryName.setText(country.getName());
43         
44         return view;
45     }
46     
47     /* ViewHolder用于对控件的实例进行缓存,优化性能
48      * 1. convertView==null, 创建ViewHolder对象,并将控件的实例存放进来
49      *    然后通过View.setTag()方法将ViewHolder对象存储在View中
50      * 2. convertView!=null, 调用View.getTag()方法,将ViewHolder对象重新取出
51      */
52     class ViewHolder {
53         ImageView countryImage;
54         TextView countryName;
55     }
56 }

在活动中使用继承的适配器,并为ListView的子项添加点击事件监听器

 1 package com.matclone.ListViewTest;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.content.Context;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.Toast;
12 import android.widget.AdapterView.OnItemClickListener;
13 import android.widget.ListView;
14 
15 public class ListViewStyle2 extends BaseActivity {
16     private List<Country> countryList = new ArrayList<Country>();
17     
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         // TODO Auto-generated method stub
21         super.onCreate(savedInstanceState);
22         this.setContentView(R.layout.listview_style2);
23         
24         createList();
25         CountryAdapter adapter = new CountryAdapter(ListViewStyle2.this, R.layout.country_item, countryList);
26         ListView lvStyle2 = (ListView) this.findViewById(R.id.lvStyle2);
27         lvStyle2.setAdapter(adapter);
28         lvStyle2.setOnItemClickListener(new OnItemClickListener() {
29             @Override
30             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
31                 // TODO Auto-generated method stub
32                 Country country = countryList.get(position);
33                 Toast.makeText(ListViewStyle2.this, country.getName(), Toast.LENGTH_SHORT).show();
34             }
35         });
36     }
37     
38     private void createList() {
39         countryList.add(new Country(R.drawable.flag_belgium, "比利时"));
40         countryList.add(new Country(R.drawable.flag_canada, "加拿大"));
41         countryList.add(new Country(R.drawable.flag_europe, "欧盟"));
42         countryList.add(new Country(R.drawable.flag_france, "法国"));
43         countryList.add(new Country(R.drawable.flag_greece, "希腊"));
44         countryList.add(new Country(R.drawable.flag_ireland, "爱尔兰"));
45         countryList.add(new Country(R.drawable.flag_italy, "意大利"));
46         countryList.add(new Country(R.drawable.flag_romania, "罗马利亚"));
47         countryList.add(new Country(R.drawable.flag_uk, "英国"));
48         countryList.add(new Country(R.drawable.flag_usa, "美国"));
49     }
50 
51 }

 

《Android第一行代码》学习记录010 - ListView用法2

原文:http://www.cnblogs.com/matclone/p/4902485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!