Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android優化listview

android優化listview

編輯:關於Android編程

public class AppManagerAdapter extends BaseAdapter {
	private static final String TAG = "AppManagerAdapter";
	private List appinfos;
	private Context context;
	//把要創建的ImageView和TextView定義在外面成靜態
	private static ImageView iv;
	private static TextView tv;

	public AppManagerAdapter(List appinfos, Context context) {
		this.appinfos = appinfos;
		this.context = context;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return appinfos.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return appinfos.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

/**
 * View convertView (轉化view對象 ,歷史view對象的緩存) convertview 就是拖動的時候被回收掉的view對象
 */
	public View getView(int position, View convertView, ViewGroup parent) {
		AppInfo info = appinfos.get(position);
		View view;
		if (convertView == null) {
			 Log.i(TAG,"通過資源文件 創建view對象");
			view = View.inflate(context, R.layout.app_item, null);
		} else {
			 Log.i(TAG,"使用歷史緩存view對象");
			view = convertView;
		}
		iv = (ImageView) view.findViewById(R.id.iv_app_icon);
		tv = (TextView) view.findViewById(R.id.tv_app_name);
		iv.setImageDrawable(info.getIcon());
		tv.setText(info.getAppname());
		return view;

	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved