I have written more recently in Adapter and I gradually become familiar with it.
Using ViewHolder is mainly used to perform some performance optimizations and reduce some unnecessary repetitions. (Teaching me by WXD.)
I won't analyze it in detail, just add the code:
public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List<MarkerItem> mMarkerData = null; public MarkerItemAdapter(Context context, List<MarkerItem> markerItems) { mContext = context; mMarkerData = markerItems; } public void setMarkerData(List<MarkerItem> markerItems) { mMarkerData = markerItems; } @Override public int getCount() { int count = 0; if (null != mMarkerData) { count = (); } return count; } @Override public MarkerItem getItem(int position) { MarkerItem item = null; if (null != mMarkerData) { item = (position); } return item; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (null == convertView) { viewHolder = new ViewHolder(); LayoutInflater mInflater = (mContext); convertView = (.item_marker_item, null); = (TextView) (); = (TextView) convertView .findViewById(); = (TextView) convertView .findViewById(); (viewHolder); } else { viewHolder = (ViewHolder) (); } // set item values to the viewHolder: MarkerItem markerItem = getItem(position); if (null != markerItem) { (()); (()); (()); } return convertView; } private static class ViewHolder { TextView name; TextView description; TextView createTime; } }
inMarkerItemis a custom class that contains fields such as name, description, createTime, etc., and has corresponding get and set methods.
ViewHolderis an internal class that contains various controls in a single project layout.
The layout of a single project, i.e. .item_marker_item, is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" android:textSize="20sp" android:textStyle="bold" /> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Description" android:textSize="18sp" /> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="CreateTime" android:textSize="16sp" /> </LinearLayout>
There is also an example in the official API Demos:
List14 in package:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Demonstrates how to write an efficient list adapter. The adapter used in this example binds * to an ImageView and to a TextView for each row in the list. * * To work efficiently the adapter implemented here uses two techniques: * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary * * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by * getView(). This data structures contains references to the views we want to bind data to, thus * avoiding calls to findViewById() every time getView() is invoked. */ public class List14 extends ListActivity { private static class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; public EfficientAdapter(Context context) { // Cache the LayoutInflate to avoid asking for a new one each time. mInflater = (context); // Icons bound to the rows. mIcon1 = ((), .icon48x48_1); mIcon2 = ((), .icon48x48_2); } /** * The number of items in the list is determined by the number of speeches * in our array. * * @see #getCount() */ public int getCount() { return ; } /** * Since the data comes from an array, just returning the index is * sufficent to get at the data. If we were using a more complex data * structure, we would return whatever object represents one row in the * list. * * @see #getItem(int) */ public Object getItem(int position) { return position; } /** * Use the array index as a unique id. * * @see #getItemId(int) */ public long getItemId(int position) { return position; } /** * Make a view to hold each row. * * @see #getView(int, , * ) */ public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = (.list_item_icon_text, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); = (TextView) (); = (ImageView) (); (holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) (); } // Bind the data efficiently with the holder. (DATA[position]); ((position & 1) == 1 ? mIcon1 : mIcon2); return convertView; } static class ViewHolder { TextView text; ImageView icon; } } @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setListAdapter(new EfficientAdapter(this)); } private static final String[] DATA = ; }
Among them the layout:
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at /licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <LinearLayout xmlns:andro android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android: android:layout_width="48dip" android:layout_height="48dip" /> <TextView android: android:layout_gravity="center_vertical" android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" /> </LinearLayout>
The above article on Android using ViewHolder to optimize the writing method of custom Adapter (must read) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.