viewholder的使用
Adapter的作用就是ListView界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。
优化的思路两种:
1. View的重用
View的每次创建是比较耗时的,因此对于getview方法传入的convertView应充分利用 != null的判断
2.ViewHolder的应用
View的findViewById()方法也是比较耗时的,因此需要考虑只调用一次,之后就用View.getTag()方法来获得ViewHolder对象。
package com.example.android_listview_activity; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; /** * @desc 自定义适配器 * @author ljt * @time 2014年8月27日 上午11:15:27 */ public class MainActivity5 extends Activity{ private static final String TAG = "MainActivity"; private ListView listView; private ImageView imageView; private TextView textView; private ViewHolder holder; private int[] images = {R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light, R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light }; private String[] names = {"北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海", "北京","上海" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)this.findViewById(R.id.listView1); MyAdapter adapter = new MyAdapter(); listView.setAdapter(adapter); } /** * @desc 自定义适配器 * @author ljt * @time 2014年8月27日 上午11:09:13 */ class MyAdapter extends BaseAdapter{ /** * How many items are in the data set represented by this Adapter. */ @Override public int getCount() { return names.length; } /** * Get the data item associated with the specified position in the data set. */ @Override public Object getItem(int position) { return names[position]; } /** * Get the row id associated with the specified position in the list. */ @Override public long getItemId(int position) { return position; } /** * 创建列表项 */ @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i(TAG, "position == "+position); if(null == convertView){ // 将layout的xml布局文件实例化为View类对象 convertView = getLayoutInflater().inflate(R.layout.source2,null); imageView = (ImageView)convertView.findViewById(R.id.imageView3); imageView.setImageResource(images[position]); textView = (TextView)convertView.findViewById(R.id.textView3); textView.setText(names[position]); holder = new ViewHolder(); holder.imageView = imageView; holder.textView = textView; convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); holder.imageView.setImageResource(images[position]); holder.textView.setText(names[position]); } return convertView; } } class ViewHolder{ private ImageView imageView; private TextView textView; } }