Android中利用ViewHolder优化自定义Adapter的典型写法(讨论ViewHolder的修饰方式)

转载 2015年07月07日 21:32:59
转载自:点击打开链接

利用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>

更多关于Adapter优化的文章:

  http://www.cnblogs.com/over140/archive/2011/03/23/1991100.html

  http://www.cnblogs.com/halzhang/archive/2010/12/05/1896791.html

 


原文中提到:
       对于这篇文章,我理解。ViewHolder的构造和复用,与静不静态没关。核心是ListView(AdapterView),通过getView(int position, View convertView, ViewGroup parent) 的convertView会为开发者传入一个可以复用的对象。开发者需要利用该对象,减少应用内存的消耗。

       如果从减少内存消耗的角度来开。我认为ViewHolder还是应该修饰成static比较好。这样ViewHolder中可以减少MainActivity的this指针,由于减少了一个this指针的引用,也会对MainActivity的引用计数大大减少。MainActivity的this指针继承于android的Context上下文,对于Context的回收遗漏,是Android内存管理中很大的问题。我们减少了对Context的引用,可以更容易减少Context引用计数出现问题。

相关文章推荐

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

用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复操作。 public class MarkerItemAdapter extends BaseAdapter { pr...

Android自定义adapter优化之ViewHolder

一般我们在写自定义adapter时,一般会继承BaseAdapter,其中会重写一个方法就是getView,让我们先看一下这个重写的方法刚出来的时候的参数。 @Override public Vie...

Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化

Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~         下面先上一个例子,是使...

Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化

Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~         下面先上一个例子,是使...

ListView(优化),自定义Layout和adapter,entity,ViewHolder,ConvertView用法案例(phone)

自定义实体类:PhoneEntity.javapackage com.example.entity;/** * Created by Administrator on 2017/2/16. */p...

自定义封装了ViewHolder的BaseAdapter,简化了Adapter的编写

自定义封装了ViewHolder的BaseAdapter,简化了Adapter的编写有过一定开发经验的Android程序员一定知道保持View对象的引用在提高ListView的性能上很有帮助,所以每次...

RecyclerView自定义基础通用Adapter和ViewHolder

RecyclerView自定义基础通用Adapter和ViewHolder仅建立一个adapter,配合自定义布局holder,轻松搞定各种RecyclerView布局使用。1、首先建立BaseVie...

使用ViewHolder模式来提高自定义adapter的效率

ViewHolder不是Android的开发API,而是一种设计方法,就是设计个静态类,缓存一下,省得Listview更新的时候,还要重新操作。 在自定义adapter时,我们得到view时可以用 ...

利用ViewHolder优化自定义BaseAdapter

动手前先讲一下为什么要用这个ViewHolder,不用这个也可以实现啊,感觉鸡肋有木有,可是实际上呢,既然发明肯定有存在的必要性,接下来就来简单讲一下BaseAdapter,慢慢就能理解为什么需要用V...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)