1.ListView在填充布局文件时优化两点; 第一、缓存; 第二。就是对填充的布局文件的控件,集中起来放在ViewHolder.将findViewById,运行次数优化。定义一个内部的ViewHold类。
//拿到listview ListView lv =(ListView)findViewById(R.id.lv); List<people> persons =Test.getPeopleData(30); MyAdpter adpter = new MyAdpter(getApplicationContext(),persons);//用构造函数携带数据,context 和数据源 lv.setAdpter(adpter); persons这里就是对数据的一个封装。
package com.zh.item.domain; import java.util.ArrayList; import java.util.List; public class Test { public static List<People> getPeopleData(int count) { List<People> persons = new ArrayList<People>(); for (int i = 0; i < count; i++) { persons.add(new People("赵" + i, "139" + i, "1000" + i)); } return persons; } } package com.zh.item.domain; public class People { private String name; private String phone; private String money; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public People(String name, String phone, String money) { super(); this.name = name; this.phone = phone; this.money = money; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } }下面定义的是是一个外部的Adapter,由于在Adpter中没有上下文对象,所以初始化时构造一个构造函数携带到用的数据。
package com.zh.item; import java.util.List; import com.zh.item.domain.People; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context context; private List<People> persons; //将context和数据源携带过来。 public MyAdapter(Context context, List<People> persons) { this.context = context; this.persons = persons; } @Override public int getCount() { // TODO Auto-generated method stub return persons.size(); //注意这个地方有时间可能会碰到空的情况(xml解析),在执行一个子线程加载数据源时此时可在数据源执行的末尾处 添加一个handler处理消息。就是在数据源加载完毕后在设置adapter(保证上面设置适配器的时间数据源加载完毕) } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub People p = persons.get(position); //数据源集合的开始从0和listview从o开始显示一样。 // // View v =LayoutInflater.from(context).inflate(R.layout.item_listview, // null); /* * LayoutInflater inflater =context.getSystemService("layout_inflater"); * inflater.inflate(R.layout.item_listview, null); */ System.out.println("get view 被调用 " + position + ";" + convertView + ":;" + parent); View v = null; ViewHolder myHolder; if (convertView == null) { v = View.inflate(context, R.layout.item_listview, null); myHolder = new ViewHolder(v); v.setTag(myHolder);//在这里setTag可以携带数据。将myholder赋给给view。 } else { v = convertView; myHolder = (ViewHolder) v.getTag(); //取出ViewHolder, 下面的赋值就不会再用到findViewById了。减少其运行的次数, } myHolder.tv_name.setText(p.getName()); myHolder.tv_phone.setText(p.getPhone()); myHolder.tv_money.setText(p.getMoney()); return v; } class ViewHolder { private TextView tv_name; private TextView tv_phone; private TextView tv_money; public ViewHolder(View v) { tv_name = (TextView) v.findViewById(R.id.tv_name); tv_phone = (TextView) v.findViewById(R.id.tv_phone); tv_money = (TextView) v.findViewById(R.id.tv_money); } } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } }