利用ViewHolder优化自定义Adapter的典型写法


1
public class MarkerItemAdapter extends BaseAdapter 2 { 3 private Context mContext = null; 4 private List<MarkerItem> mMarkerData = null; 5 6 public MarkerItemAdapter(Context context, List<MarkerItem> markerItems) 7 { 8 mContext = context; 9 mMarkerData = markerItems; 10 } 11 12 public void setMarkerData(List<MarkerItem> markerItems) 13 { 14 mMarkerData = markerItems; 15 } 16 17 @Override 18 public int getCount() 19 { 20 int count = 0; 21 if (null != mMarkerData) 22 { 23 count = mMarkerData.size(); 24 } 25 return count; 26 } 27 28 @Override 29 public MarkerItem getItem(int position) 30 { 31 MarkerItem item = null; 32 33 if (null != mMarkerData) 34 { 35 item = mMarkerData.get(position); 36 } 37 38 return item; 39 } 40 41 @Override 42 public long getItemId(int position) 43 { 44 return position; 45 } 46 47 @Override 48 public View getView(int position, View convertView, ViewGroup parent) 49 { 50 ViewHolder viewHolder = null; 51 if (null == convertView) 52 { 53 viewHolder = new ViewHolder(); 54 LayoutInflater mInflater = LayoutInflater.from(mContext); 55 convertView = mInflater.inflate(R.layout.item_marker_item, null); 56 57 viewHolder.name = (TextView) convertView.findViewById(R.id.name); 58 viewHolder.description = (TextView) convertView 59 .findViewById(R.id.description); 60 viewHolder.createTime = (TextView) convertView 61 .findViewById(R.id.createTime); 62 63 convertView.setTag(viewHolder); 64 } 65 else 66 { 67 viewHolder = (ViewHolder) convertView.getTag(); 68 } 69 70 // set item values to the viewHolder: 71 72 MarkerItem markerItem = getItem(position); 73 if (null != markerItem) 74 { 75 viewHolder.name.setText(markerItem.getName()); 76 viewHolder.description.setText(markerItem.getDescription()); 77 viewHolder.createTime.setText(markerItem.getCreateDate()); 78 } 79 80 return convertView; 81 } 82 83 private static class ViewHolder 84 { 85 TextView name; 86 TextView description; 87 TextView createTime; 88 } 89 90 }

convertView及缓存的View,若convertView!=null,则不必反射view。viewHolder是一个内部类,用于对控件的实例进行缓存。当convertView为空时,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder中,然后调用View的setTag()方法,将ViewHolder对象存储在View中。当convertView不为空时则调用View的getTag()方法,把ViewHolder重新取出。

 

其中MarkerItem是自定义的类,其中包含name,description,createTime等字段,并且有相应的get和set方法。

 

  ViewHolder是一个内部类,其中包含了单个项目布局中的各个控件。

  单个项目的布局,即R.layout.item_marker_item如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" 
 6     android:padding="5dp">
 7 
 8     <TextView
 9         android:id="@+id/name"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:text="Name"
13         android:textSize="20sp"
14         android:textStyle="bold" />
15 
16     <TextView
17         android:id="@+id/description"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:text="Description"
21         android:textSize="18sp" />
22 
23     <TextView
24         android:id="@+id/createTime"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:text="CreateTime"
28         android:textSize="16sp" />
29 
30 </LinearLayout>

官方的API Demos中也有这个例子:

package com.example.android.apis.view中的List14:

  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;
141 }

其中布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- Copyright (C) 2007 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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18     android:orientation="horizontal"
19     android:layout_width="match_parent"
20     android:layout_height="match_parent">
21 
22     <ImageView android:id="@+id/icon"
23         android:layout_width="48dip"
24         android:layout_height="48dip" />
25 
26     <TextView android:id="@+id/text"
27         android:layout_gravity="center_vertical"
28         android:layout_width="0dip"
29         android:layout_weight="1.0"
30         android:layout_height="wrap_content" />
31 
32 </LinearLayout>

转自:http://www.cnblogs.com/mengdd/p/3254323.html

posted @ 2016-02-15 16:13  鸭子船长  阅读(308)  评论(0编辑  收藏  举报