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

 更新时间:2017年04月18日 10:07:11   投稿:jingxian  
下面小编就为大家带来一篇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的写法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android编程实现监控apk安装,卸载,替换的方法

    Android编程实现监控apk安装,卸载,替换的方法

    这篇文章主要介绍了Android编程实现监控apk安装,卸载,替换的方法,涉及Android基于Intent监控apk状态的功能实现技巧,需要的朋友可以参考下
    2016-01-01
  • Android 使用ViewPager实现左右循环滑动及轮播效果

    Android 使用ViewPager实现左右循环滑动及轮播效果

    ViewPager是一个常用的Android组件,不过通常我们使用ViewPager的时候不能实现左右无限循环滑动,在滑到边界的时候会看到一个不能翻页的动画,可能影响用户体验,接下来通过本文给大家介绍Android 使用ViewPager实现左右循环滑动及轮播效果,一起看看吧
    2017-02-02
  • Android Canvas的drawText()与文字居中方案详解

    Android Canvas的drawText()与文字居中方案详解

    这篇文章主要给大家介绍了关于Android Canvas的drawText()与文字居中方案的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • Android创建Menu菜单实例

    Android创建Menu菜单实例

    这篇文章主要介绍了Android创建Menu菜单实例,讲述了Android菜单项的创建方法,在Android应用程序开发中非常具有实用价值,需要的朋友可以参考下
    2014-10-10
  • Android几种多渠道打包的步骤详解

    Android几种多渠道打包的步骤详解

    在不同的应用市场可能有不同的统计需求,需要为每个应用市场发布一个安装包,这里就引出了Android的多渠道打包。这篇文章主要介绍了Android几种多渠道打包,需要的朋友可以参考下
    2019-09-09
  • Android仿微信朋友圈全文、收起功能的实例代码

    Android仿微信朋友圈全文、收起功能的实例代码

    本篇文章主要介绍了Android仿微信朋友圈全文、收起功能的实例代码,具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08
  • Android中的Handler与多线程应用实例

    Android中的Handler与多线程应用实例

    这篇文章主要介绍了Android中的Handler与多线程应用实例,本文首先解释一下handler是用来干嘛的,然后通过例子介绍其在多线程中的应用,需要的朋友可以参考下
    2015-03-03
  • Android Secret Code(输入字符弹出手机信息)详解

    Android Secret Code(输入字符弹出手机信息)详解

    这篇文章主要介绍了Android Secret Code(输入字符弹出手机信息)详解的相关资料,需要的朋友可以参考下
    2016-11-11
  • Android ViewPagerIndicator详解及实例代码

    Android ViewPagerIndicator详解及实例代码

    这篇文章主要介绍了Android ViewPagerIndicator详解及实例代码的相关资料,需要的朋友可以参考下
    2017-05-05
  • Flutter实现视频压缩功能的示例代码

    Flutter实现视频压缩功能的示例代码

    移动应用程序中,视频占用了大量的存储空间和带宽,这在一定程度上影响了应用程序的性能和用户体验,所以本文为大家准备了Flutter实现视频压缩的方法,需要的可以参考一下
    2023-06-06

最新评论