優(yōu)化SimpleAdapter適配器加載效率的方法
更新時間:2017年04月18日 10:00:58 投稿:jingxian
下面小編就為大家?guī)硪黄獌?yōu)化SimpleAdapter適配器加載效率的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在主Activity中:
listview=(ListView)findViewById(R.id.listview);getData(); //為list添加數(shù)據(jù)overrideSimpleAdapter=new OverrideSimpleAdapter(getContext(),list,R.layout.list_item_layout, new String[]{"num","word","translates"}, new int[]{R.id.tv_num,R.id.tv_word,R.id.tv_translates}); listview.setAdapter(overrideSimpleAdapter); 重寫SimpleAdapter:/** * Created by KewenC on 2017/1/26. */ public class OverrideSimpleAdapter extends SimpleAdapter { /** * Constructor * * @param context The context where the View associated with this SimpleAdapter is running * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The * Maps contain the data for each row, and should include all the entries specified in * "from" * @param resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param from A list of column names that will be added to the Map associated with each * item. * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns */ private LayoutInflater mInflater; private ArrayList<Map<String, Object>> list; private int mResource; private int[] mTo; private String[] mFrom; public OverrideSimpleAdapter(Context context, ArrayList<Map<String, Object>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.list=data; this.mInflater = LayoutInflater.from(context); this.mResource = resource; this.mFrom = from; this.mTo = to; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; // 判斷是否緩存 if (convertView == null) { holder = new ViewHolder(); // 通過LayoutInflater實例化布局 convertView = mInflater.inflate(mResource, null); // holder.img = (ImageView) convertView.findViewById(R.id.imageView); holder.num = (TextView) convertView.findViewById(mTo[0]); holder.word = (TextView) convertView.findViewById(mTo[1]); holder.translates = (TextView) convertView.findViewById(mTo[2]); convertView.setTag(holder); } else { // 通過tag找到緩存的布局 holder = (ViewHolder) convertView.getTag(); } // 設(shè)置布局中控件要顯示的視圖 // holder.img.setBackgroundResource(R.drawable.ic_launcher); holder.num.setText(list.get(position).get(mFrom[0]).toString());// mFrom[0]為“num”Key holder.word.setText(list.get(position).get(mFrom[1]).toString()); holder.translates.setText(list.get(position).get(mFrom[2]).toString()); return convertView; } public final class ViewHolder { // public ImageView img; public TextView num; public TextView word; public TextView translates; } }
以上這篇優(yōu)化SimpleAdapter適配器加載效率的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
這篇文章主要介紹了android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android zxing如何識別反轉(zhuǎn)二維碼詳解
這篇文章主要給大家介紹了關(guān)于Android zxing如何識別反轉(zhuǎn)二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09android實現(xiàn)多線程下載文件(支持暫停、取消、斷點續(xù)傳)
本篇文章主要介紹了androids實現(xiàn)多線程下載文件,主要包括暫停、取消、斷點續(xù)傳等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02