官方参考优化代码:

{sdk}\samples\android-16\ApiDemos\src\com\example\android\apis\view\list14.java


0.mInflater缓存全局的Inflater,避免每次获取。

mInflater = LayoutInflater.from(context)

1. 利用convertView避免重复inflate view

2. 利用ViewHolder缓存相应的控件,避免每次findViewById


/* * 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;}

参考:

Android开发之ListView 适配器(Adapter)优化

1.http://www.cnblogs.com/error404/archive/2011/08/03/2126682.html

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

3.http://www.eoeandroid.com/thread-254866-1-1.html



更多相关文章

  1. Android实现系统打印功能
  2. android按屏幕大小动态确定控件位置及大小
  3. UI 开源代码 FileBrowserView
  4. Android(安卓)初始化NavigationView的headerLayout中的TextView
  5. android OOM的那些事
  6. Android(安卓)带清除功能的输入框控件实例详解
  7. android 自定义控件之折线图自己写代码,不用jar包
  8. Android打造万能适配器--RecyclerView
  9. Android(安卓)ListView控件使用

随机推荐

  1. Android之短信广播接收内容
  2. Android(安卓)X86 获取不到ip的解决方法
  3. EditText 监听 Text 的 改动
  4. [置顶] 基础总结篇:Intent应用详解
  5. Android图形显示之本地窗口
  6. Android 查看手机中所有进程
  7. Android教你一步一步实现简单重用自定义
  8. Delphi XE5 for android 使用 BITMAP STY
  9. eclipse + android sdk开发环境中svn配置
  10. android创建aidl小实例