Android中利用ViewHolder优化自定义Adapter的写法(必看)

Android中利用ViewHolder优化自定义Adapter的写法(必看),第1张

概述最近写Adapter写得多了,慢慢就熟悉了。用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复 *** 作。(WXD同学教我的。)

最近写Adapter写得多了,慢慢就熟悉了。

用VIEwHolder,主要是进行一些性能优化,减少一些不必要的重复 *** 作。(WXD同学教我的。)

具体不分析了,直接上一份代码吧:

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 = mMarkerData.size();    }    return count;  }  @OverrIDe  public MarkerItem getItem(int position)  {    MarkerItem item = null;    if (null != mMarkerData)    {      item = mMarkerData.get(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 = LayoutInflater.from(mContext);      convertVIEw = mInflater.inflate(R.layout.item_marker_item,null);      vIEwHolder.name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.name);      vIEwHolder.description = (TextVIEw) convertVIEw          .findVIEwByID(R.ID.description);      vIEwHolder.createTime = (TextVIEw) convertVIEw          .findVIEwByID(R.ID.createTime);      convertVIEw.setTag(vIEwHolder);    }    else    {      vIEwHolder = (VIEwHolder) convertVIEw.getTag();    }    // set item values to the vIEwHolder:    MarkerItem markerItem = getItem(position);    if (null != markerItem)    {      vIEwHolder.name.setText(markerItem.getname());      vIEwHolder.description.setText(markerItem.getDescription());      vIEwHolder.createTime.setText(markerItem.getCreateDate());    }    return convertVIEw;  }  private static class VIEwHolder  {    TextVIEw name;    TextVIEw description;    TextVIEw createTime;  }}

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

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

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

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical"   androID:padding="5dp">  <TextVIEw    androID:ID="@+ID/name"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="name"    androID:textSize="20sp"    androID:textStyle="bold" />  <TextVIEw    androID:ID="@+ID/description"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="Description"    androID:textSize="18sp" />  <TextVIEw    androID:ID="@+ID/createTime"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="CreateTime"    androID:textSize="16sp" /></linearLayout>

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

package com.example.androID.APIs.vIEw中的List14:

/* * 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 * *   http://www.apache.org/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 com.example.androID.APIs.vIEw;import androID.app.ListActivity;import androID.content.Context;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.TextVIEw;import androID.Widget.ImageVIEw;import androID.graphics.BitmapFactory;import androID.graphics.Bitmap;import com.example.androID.APIs.R;/** * 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 = LayoutInflater.from(context);      // Icons bound to the rows.      mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_1);      mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_2);    }    /**     * The number of items in the List is determined by the number of speeches     * in our array.     *     * @see androID.Widget.listadapter#getCount()     */    public int getCount() {      return DATA.length;    }    /**     * 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 androID.Widget.listadapter#getItem(int)     */    public Object getItem(int position) {      return position;    }    /**     * Use the array index as a unique ID.     *     * @see androID.Widget.listadapter#getItemID(int)     */    public long getItemID(int position) {      return position;    }    /**     * Make a vIEw to hold each row.     *     * @see androID.Widget.listadapter#getVIEw(int,androID.vIEw.VIEw,*   androID.vIEw.VIEwGroup)     */    public VIEw getVIEw(int position,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 = mInflater.inflate(R.layout.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();        holder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.text);        holder.icon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.icon);        convertVIEw.setTag(holder);      } else {        // Get the VIEwHolder back to get fast access to the TextVIEw        // and the ImageVIEw.        holder = (VIEwHolder) convertVIEw.getTag();      }      // Bind the data efficIEntly with the holder.      holder.text.setText(DATA[position]);      holder.icon.setimageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);      return convertVIEw;    }    static class VIEwHolder {      TextVIEw text;      ImageVIEw icon;    }  }  @OverrIDe  public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setlistadapter(new EfficIEntAdapter(this));  }  private static final String[] DATA = Cheeses.sCheeseStrings;}

其中布局:

<?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      http://www.apache.org/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:androID="http://schemas.androID.com/apk/res/androID"  androID:orIEntation="horizontal"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <ImageVIEw androID:ID="@+ID/icon"    androID:layout_wIDth="48dip"    androID:layout_height="48dip" />  <TextVIEw androID:ID="@+ID/text"    androID:layout_gravity="center_vertical"    androID:layout_wIDth="0dip"    androID:layout_weight="1.0"    androID:layout_height="wrap_content" /></linearLayout>

以上这篇AndroID中利用VIEwHolder优化自定义Adapter的写法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android中利用ViewHolder优化自定义Adapter的写法(必看)全部内容,希望文章能够帮你解决Android中利用ViewHolder优化自定义Adapter的写法(必看)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存