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

最近写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, 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 = 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的写法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • android listview优化几种写法详细介绍

    这篇文章只是总结下getView里面优化视图的几种写法,就像孔乙己写茴香豆的茴字的几种写法一样,高手勿喷,勿笑,只是拿出来分享,有错误的地方欢迎大家指正,谢谢. listview Aviewthatshowsitemsinaverticallyscrollinglist. 一个显示一个垂直的滚动子项的列表视图在android开发中,使用listview的地方很多,用它来展现数据,成一个垂直的视图.使用listview是一个标准的适配器模式,用数据--,界面--xml以及适配器--adapter,

  • Android自定义Adapter的ListView的思路及代码

    在开发中,我们经常使用到ListView这个控件.Android的API也提供了许多创建ListView适配器的快捷方式.例如ArrayAdapter.SimpleAdapter和SimpleCursorAdapter等.但你是否发现,如果采用这些系统自带的适配器,对于事件的响应只能局限在一个行单位.假设一行里面有一个按钮和一个图片控件,它们之间的响应操作是不一样的.若采用系统自带的适配器,就不能精确到每个控件的响应事件.这时,我们一般采取自定义适配器来实现这个比较精确地请求. ListView

  • Android开发中ListView自定义adapter的封装

    [引入] 我们一般编写listView的时候顺序是这样的:  •需要展示的数据集List<T>  •为这个数据集编写一个ListView  •为这个ListView编写一个Adapter,一般继承自BaseAdapter  •在BaseAdapter内部编写一个ViewHolder类,对应ListView里面的item控件,提高控件的查询效率 分析: List<T>:ListView --> Adapter extends BaseAdapter --> ViewHol

  • Android自定义实现BaseAdapter的优化布局

    上一篇中我们介绍了自定义实现BaseAdapter的普通实现布局,然而上一章也说了普通实现的方式效率会很低,而且对系统开销也很大,所以,那样的实现是为了让初学者能知道可以这样使用,在实际项目中不可能使用那种方式的,要是你在做项目的时候使用普通布局方式,我敢保证,不过试用期你的老板就给你飞机票走人了,好了,闲话少说,本次讲解一下优化布局的实现,看完代码后,你会觉得,其实很简单. MainActivity.java public class MainActivity extends AppCompa

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

    最近写Adapter写得多了,慢慢就熟悉了. 用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复操作.(WXD同学教我的.) 具体不分析了,直接上一份代码吧: public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List<MarkerItem> mMarkerData = null; public MarkerItemAdapter(Cont

  • Android 通过ViewHolder优化适配器的实现方法(必看)

    Adapter类的定义: Adapter对象是AdapterView和底层数据见的桥梁.Adapter用于访问数据项,并且负责为数据项生成视图 AdapterView是一个抽象类,用于那些需要通过Adapter填充自身的视图,其常见子类是ListView.显示AdapterView时会调用Adapter的getView()方法创建并添加每个子条目的视图.Adapter的getView()方法就是用来创建这些视图的,Adapter并不会为每行数据都创建一个新视图,而是提供了回收旧视图的方法.运行机

  • 详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题

    详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题 今天在项目中实现了一个效果,主要是画一个圆.为了后续使用方便,将这个圆封装在一个自定义Actor(CircleActot)中,后续想显示一个圆的时候,只要创建一个CircleActor中即可. 部分代码如下所示: package com.ef.smallstar.unitmap.widget; import android.content.res.Resources; import

  • Android中利用动态加载实现手机淘宝的节日特效

    相信去年圣诞节打开过手机淘宝的童鞋都会对当时的特效记忆犹新吧:全屏飘雪,旁边还有个小雪人来控制八音盒背景音乐的播放,让人有种身临其境的感觉,甚至忍不住想狠狠购物了呢(误),大概就是下面这个样子滴: 嗯,确实很炫,那么我们一步步去分析是如何实现的: 一.实现下雪的 View 首先,最上面一层的全屏雪花极有可能是一个顶层的View,而这个View是通过动态加载去控制显示的(不更新淘宝也能看到这个效果).那么我们先得实现雪花效果的 View,人生苦短,拿来就用.打开 gank.io,搜索"雪花&quo

  • Android中利用viewflipper动画切换屏幕效果

    整个项目的 package com.example.viewflipper; import android.R.integer; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector.OnDoubleTapListener; import android.view.Menu; import android.view.Me

  • Android中利用NetworkInfo判断网络状态时出现空指针(NullPointerException)问题的解决方法

    在Android中,很多人会用如下的方法判断当前网络是否可用: /** * 获取当前网络状态(是否可用) */ public static boolean isNetworkAvailable() { boolean isAalable = false; ConnectivityManager connManager = (ConnectivityManager) BaseApplication.getApplication().getSystemService(Context.CONNECTI

  • Android中利用C++处理Bitmap对象的实现方法

    相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码的运行速度太慢,而一副很小的240*320图像就有76800个像素,如果考虑到RGB三通道(或者ARGB四通道),还要对这个数量乘以3/4.因此对图像的密集计算一般都利用Jni接口,用C++实现.那么问题来了,怎么把Bitmap中的像素数据从Java层传到C++层? 做法1:之前的做法 我之前的做法是这样的

  • java中使用try-catch-finally一些值得注意的事(必看)

    我们知道,try负责圈定可能会出异常的代码:catch负责处理try中可能异常的处理,如记录错误日志,使业务能够正常运行:finally负责资源释放等善后工作,无论有无异常都必须要执行的代码,一般都是放在finally中的.如果catch和finally也会出现异常,那么会是什么效果呢? try { // java.lang.ArithmeticException int a = 1 / 0; } catch (Exception e) { System.out.println("catch&q

  • SQL中实现SPLIT函数几种方法总结(必看篇)

    例1 代码如下 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10)) returns @temp table(a varchar(100)) --实现split功能 的函数 --date :2003-10-14 as begin declare @i int set @SourceSql=rtrim(ltrim(@SourceSql)) set @i=charindex(@StrSeprate,@Sou

  • 根据list中对象的属性去重和排序小结(必看篇)

    如下所示: //去重 public class User { private int id; private String name; private int age; public User(){} public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void

随机推荐