Android之ViewHolder用法

   先声明一下ViewHolder在Android自定义的适配器中使用。目的:优化资源,节省空间,避免重复绘制view而引起的不必要的内存损耗。

我自己以前的写法:

  1. public class PlateAdapter extends BaseAdapter {  
  2.       
  3.     private List<Plate> list;  
  4.     private Context context;  
  5.       
  6.     public PlateAdapter(List<Plate> list, Context context) {  
  7.         super();  
  8.         this.list = list;  
  9.         this.context = context;  
  10.     }  
  11.   
  12.     @Override  
  13.     public int getCount() {  
  14.         // TODO Auto-generated method stub  
  15.         return list.size();  
  16.     }  
  17.   
  18.     @Override  
  19.     public Object getItem(int arg0) {  
  20.         // TODO Auto-generated method stub  
  21.         return list.get(arg0);  
  22.     }  
  23.   
  24.     @Override  
  25.     public long getItemId(int arg0) {  
  26.         // TODO Auto-generated method stub  
  27.         return arg0;  
  28.     }  
  29.   
  30.     @Override  
  31.     public View getView(int arg0, View arg1, ViewGroup arg2) {  
  32.         // TODO Auto-generated method stub  
  33.         if(arg1 == null){  
  34.             arg1 = LayoutInflater.from(context).inflate(R.layout.select_car_type_list_item, null);  
  35.         }  
  36.         TextView text = (TextView)arg1.findViewById(R.id.text);  
  37.         text.setText(list.get(arg0).getPlateType());  
  38.         return arg1;  
  39.     }  
  40.   
  41. }  
学习过ViewHolder之后的写法:
  1. @SuppressWarnings("unused")  
  2. public class NoticeAdapter  extends BaseAdapter{  
  3.     private Context _context;  
  4.     private List<ExamNotice> _list;  
  5.       
  6.     public NoticeAdapter(Context context, List<ExamNotice> list) {  
  7.         super();  
  8.         this._context = context;  
  9.         this._list = list;  
  10.     }  
  11.   
  12.     public void set_list(List<ExamNotice> _list) {  
  13.         this._list = _list;  
  14.     }  
  15.   
  16.     @Override  
  17.     public int getCount() {  
  18.         return _list.size();  
  19.     }  
  20.   
  21.     @Override  
  22.     public Object getItem(int arg0) {  
  23.         return _list.get(arg0);  
  24.     }  
  25.   
  26.     @Override  
  27.     public long getItemId(int arg0) {  
  28.         return arg0;  
  29.     }  
  30.   
  31.     @Override  
  32.     public View getView(int position, View convertView, ViewGroup parent) {  
  33.         Holder holder;  
  34.         if(null==convertView){  
  35.             convertView=View.inflate(_context, R.layout.notice_item, null);  
  36.             holder=new Holder();  
  37.             holder.studyPlanName=(TextView)convertView.findViewById(R.id.xxjh_item_name);  
  38.             holder.studyPlanDate=(TextView)convertView.findViewById(R.id.xxjh_item_date);  
  39.             convertView.setTag(holder);  
  40.         }else{  
  41.             holder=(Holder)convertView.getTag();  
  42.         }  
  43.         ExamNotice notice=(ExamNotice) getItem(position);  
  44.         holder.studyPlanName.setText(notice.getNoticeTitle());  
  45.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  46.         String noticeDate = sdf.format(notice.getNoticeDate());  
  47.           
  48.         holder.studyPlanDate.setText(noticeDate);  
  49.         return convertView;  
  50.     }  
  51.     private static class Holder{  
  52.         public TextView studyPlanName,studyPlanDate;  
  53.     }  
  54.   
  55. }  

看一下官方的API:

  1. /*  
  2.  * Copyright (C) 2008 The Android Open Source Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */  
  16.   
  17. package com.example.android.apis.view;  
  18.   
  19. import android.app.ListActivity;  
  20. import android.content.Context;  
  21. import android.os.Bundle;  
  22. import android.view.LayoutInflater;  
  23. import android.view.View;  
  24. import android.view.ViewGroup;  
  25. import android.widget.BaseAdapter;  
  26. import android.widget.TextView;  
  27. import android.widget.ImageView;  
  28. import android.graphics.BitmapFactory;  
  29. import android.graphics.Bitmap;  
  30. import com.example.android.apis.R;  
  31.   
  32. /**  
  33.  * Demonstrates how to write an efficient list adapter. The adapter used in this example binds  
  34.  * to an ImageView and to a TextView for each row in the list.  
  35.  *  
  36.  * To work efficiently the adapter implemented here uses two techniques:  
  37.  * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary  
  38.  * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary  
  39.  *  
  40.  * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by  
  41.  * getView(). This data structures contains references to the views we want to bind data to, thus  
  42.  * avoiding calls to findViewById() every time getView() is invoked.  
  43.  */  
  44. public class List14 extends ListActivity {  
  45.   
  46.     private static class EfficientAdapter extends BaseAdapter {  
  47.         private LayoutInflater mInflater;  
  48.         private Bitmap mIcon1;  
  49.         private Bitmap mIcon2;  
  50.   
  51.         public EfficientAdapter(Context context) {  
  52.             // Cache the LayoutInflate to avoid asking for a new one each time.  
  53.             mInflater = LayoutInflater.from(context);  
  54.   
  55.             // Icons bound to the rows.  
  56.             mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);  
  57.             mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);  
  58.         }  
  59.   
  60.         /**  
  61.          * The number of items in the list is determined by the number of speeches  
  62.          * in our array.  
  63.          *  
  64.          * @see android.widget.ListAdapter#getCount()  
  65.          */  
  66.         public int getCount() {  
  67.             return DATA.length;  
  68.         }  
  69.   
  70.         /**  
  71.          * Since the data comes from an array, just returning the index is  
  72.          * sufficent to get at the data. If we were using a more complex data  
  73.          * structure, we would return whatever object represents one row in the  
  74.          * list.  
  75.          *  
  76.          * @see android.widget.ListAdapter#getItem(int)  
  77.          */  
  78.         public Object getItem(int position) {  
  79.             return position;  
  80.         }  
  81.   
  82.         /**  
  83.          * Use the array index as a unique id.  
  84.          *  
  85.          * @see android.widget.ListAdapter#getItemId(int)  
  86.          */  
  87.         public long getItemId(int position) {  
  88.             return position;  
  89.         }  
  90.   
  91.         /**  
  92.          * Make a view to hold each row.  
  93.          *  
  94.          * @see android.widget.ListAdapter#getView(int, android.view.View,  
  95.          *      android.view.ViewGroup)  
  96.          */  
  97.         public View getView(int position, View convertView, ViewGroup parent) {  
  98.             // A ViewHolder keeps references to children views to avoid unneccessary calls  
  99.             // to findViewById() on each row.  
  100.             ViewHolder holder;  
  101.   
  102.             // When convertView is not null, we can reuse it directly, there is no need  
  103.             // to reinflate it. We only inflate a new View when the convertView supplied  
  104.             // by ListView is null.  
  105.             if (convertView == null) {  
  106.                 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);  
  107.   
  108.                 // Creates a ViewHolder and store references to the two children views  
  109.                 // we want to bind data to.  
  110.                 holder = new ViewHolder();  
  111.                 holder.text = (TextView) convertView.findViewById(R.id.text);  
  112.                 holder.icon = (ImageView) convertView.findViewById(R.id.icon);  
  113.   
  114.                 convertView.setTag(holder);  
  115.             } else {  
  116.                 // Get the ViewHolder back to get fast access to the TextView  
  117.                 // and the ImageView.  
  118.                 holder = (ViewHolder) convertView.getTag();  
  119.             }  
  120.   
  121.             // Bind the data efficiently with the holder.  
  122.             holder.text.setText(DATA[position]);  
  123.             holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);  
  124.   
  125.             return convertView;  
  126.         }  
  127.   
  128.         static class ViewHolder {  
  129.             TextView text;  
  130.             ImageView icon;  
  131.         }  
  132.     }  
  133.   
  134.     @Override  
  135.     public void onCreate(Bundle savedInstanceState) {  
  136.         super.onCreate(savedInstanceState);  
  137.         setListAdapter(new EfficientAdapter(this));  
  138.     }  
  139.   
  140.     private static final String[] DATA = Cheeses.sCheeseStrings;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值