Easy Tutorial
❮ All Vim Cheatsheat Python Variable References And Copies ❯

2.4.6 BaseAdapter Optimization

Category Android Basic Tutorial

Introduction:

>

In the previous section, we learned how to use a ListView and customize a simple BaseAdapter. From the code, we can see two significant methods: getCount() and getView(). The getView() method is called as many times as there are items in the list. This reveals a potential issue: each call to getView() inflates a new View, which involves parsing the XML layout, leading to resource waste. While this may not be noticeable with a few dozen or hundred items, it becomes a problem with more items or more complex layouts. Therefore, optimizing ListView is crucial, and this section focuses on optimizing BaseAdapter. The optimizations include reusing convertView and using ViewHolder to reuse components, avoiding repeated calls to findViewById(). Let's see how this works in code!


1. Reusing ConvertView:

>

As mentioned, getView() is called for each item in the list. Let's look at the getView() code from the previous section:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal, parent, false);
    ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
    TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
    TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);

    img_icon.setBackgroundResource(mData.get(position).getaIcon());
    txt_aName.setText(mData.get(position).getaName());
    txt_aSpeak.setText(mData.get(position).getaSpeak());
    return convertView;
}

>

Notice that inflate() loads the XML layout each time. The convertView provided by the system is a cached object of a reusable View. We can add a check to optimize this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal, parent, false);
    }

    ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
    TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
    TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);

    img_icon.setBackgroundResource(mData.get(position).getaIcon());
    txt_aName.setText(mData.get(position).getaName());
    txt_aSpeak.setText(mData.get(position).getaSpeak());
    return convertView;
}

2. ViewHolder to Reuse Components

>

Since getView() is called multiple times, findViewById() is also called multiple times. Since the ListView items usually have the same layout, we can optimize this by creating a ViewHolder class to store the views:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal, parent, false);
        holder = new ViewHolder();
        holder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
        holder.txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
        holder.txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);
        convertView.setTag(holder);   // Store the holder in convertView
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.img_icon.setBackgroundResource(mData.get(position).getaIcon());
    holder.txt_aName.setText(mData.get(position).getaName());
    holder.txt_aSpeak.setText(mData.get(position).getaSpeak());
    return convertView;
}

static class ViewHolder {
    ImageView img_icon;
    TextView txt_aName;
    TextView txt_aSpeak;
}

>

That's it! You can follow this template for BaseAdapter. The static modifier for ViewHolder is used to ensure the class is loaded only once, regardless of the number of objects. This is a recommendation from Berial (B神).


Summary:

>

The optimizations for BaseAdapter are straightforward: reusing convertView and creating a ViewHolder to reduce calls to findViewById(). If you have other suggestions for optimizing BaseAdapter, please share them. Thank you!

-1.0 Android Basic Tutorial

-1.0.1 2015 Latest Android Basic Tutorial Contents

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Using Eclipse + ADT + SDK to Develop Android Apps

-1.2.2 Using Android Studio to Develop Android Apps

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial: Basic Operations on Local Repository

-1.5.2 Git: Setting Up a Remote Repository on GitHub

-1.6 How to Use 9-Patch Images

-1.7 Interface Prototype Design

-1.8 Project Source Analysis (Various Files, Resource Access)

-1.9 Signing and Packaging Android Applications

-1.11 Decompiling APK to Retrieve Code and Resources

-2.1 Concepts of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 Detailed Explanation of TextView (Text View)

-2.3.2 Detailed Explanation of EditText (Input Box)

-2.3.3 Button and ImageButton

-2.3.4 ImageView (Image View)

-2.3.5.RadioButton (Radio Button) & Checkbox (Checkbox)

-8.1.2 Summary of 13 Drawable Types in Android Part 2

-8.1.3 Summary of 13 Drawable Types in Android Part 3

-8.2.1 Comprehensive Analysis of Bitmap (Bitmaps) Part 1

-8.2.2 OOM Issues Caused by Bitmap

-8.3.1 Detailed Explanation of Three Drawing Tool Classes

-8.3.2 Practical Examples of Drawing Classes

-8.3.3 Paint API — MaskFilter (Mask)

-8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 1)

-8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 2)

-8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 3)

-8.3.7 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 4)

-8.3.8 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 5)

-8.3.9 Paint API — ColorFilter (Color Filter) (1/3)

-8.3.10 Paint API — ColorFilter (Color Filter) (2/3)

-8.3.11 Paint API — ColorFilter (Color Filter) (3/3)

-8.3.12 Paint API — PathEffect (Path Effect)

-8.3.13 Paint API — Shader (Image Rendering)

-8.3.14 Paint Enumerations/Constants and ShadowLayer Shadow Effect

-8.3.15 Paint API — Typeface (Font Style)

-8.3.16 Canvas API Detailed Explanation (Part 1)

-8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Methods Collection

-8.3.18 Canvas API Detailed Explanation (Part 3) Matrix and drawBitmapMesh

-8.4.1 Frame Animation Collection in Android

-8.4.2 Tween Animation Collection in Android

-8.4.3 Property Animation in Android - First Encounter

-8.4.4 Property Animation in Android - Second Encounter

-9.1 Playing Sound Effects with SoundPool (Duang~)

-9.2 MediaPlayer for Audio and Video Playback

-9.3 Using Camera to Take Photos

-9.4 Using MediaRecord for Recording

-10.1 TelephonyManager (Telephony Manager)

-10.2 SmsManager (SMS Manager)

-10.3 AudioManager (Audio Manager)

-10.4 Vibrator (Vibrator)

-10.5 AlarmManager (Alarm Service)

-10.6 PowerManager (Power Service)

-10.7 WindowManager (Window Management Service)

-10.8 LayoutInflater (Layout Service)

-10.9 WallpaperManager (Wallpaper Manager)

-10.10 Sensor Topics (1) — Introduction

-10.11 Sensor Topics (2) — Orientation Sensor

-10.12 Sensor Topics (3) — Accelerometer/Gyroscope Sensor

-10.12 Sensor Topics (4) — Understanding Other Sensors

-10.14 Introduction to Android GPS

-11.0《2015 Latest Android Basic Beginner's Tutorial》Completion Celebration~

-12.1 Android Practice: DrySister Viewing Girls App (First Edition) — Project Setup and Simple Implementation

-12.2 DrySister Viewing Girls App (First Edition) — 2. Parsing Backend Data

-12.3 DrySister Viewing Girls App (First Edition) — 3. Image Loading Optimization (Writing a Small Image Cache Framework)

-12.4 DrySister Viewing Girls App (First Edition) — 4. Adding Data Caching (Introducing SQLite)

-12.5 DrySister Viewing Girls App (First Edition) — 5. Code Review, Adjustment, and Logging Class Writing

-12.6 DrySister Viewing Girls App (First Edition) — 6. Icon Creation, Obfuscation, Signing, APK Thinning, App Release

WeChat Subscription

❮ All Vim Cheatsheat Python Variable References And Copies ❯