Performance Tips for Android’s ListView

本文介绍如何通过视图回收、ViewHolder模式、异步加载和交互感知等技巧提高Android中ListView组件的性能表现。

I’ve been messing around with Android-based code for a few months now while hacking on Native Firefox for Android and Pattrn. I noticed that the performance tips for ListViews are a bit scattered in different sources. This post is an attempt to summarize the ones I found most useful.

I’m assuming you’re already familiar with ListViews and understand the framework around AdapterViews. I’ve added some Android source code pointers for the curious readers willing to understand things a bit deeper.

How it works

ListView is designed for scalability and performance. In practice, this essentially means:

  1. It tries to do as few view inflations as possible.
  2. It only paints and lays out children that are (or are about to become) visible on screencode.

The reason for 1 is simple: layout inflations are expensive operationscode. Although layout files are compiled into binary form for more efficient parsingcode, inflations still involve going through a tree of special XML blockscode and instantiating all respective views. ListView solves this problem by recyclingcode non-visible views—called “ScrapViews” in Android’s source code—as you pan around. This means that developers can simply update the contents of recycled viewscode instead of inflating the layout of every single row—more on that later.

In order to implement 2, ListView uses the view recycler to keep adding recycled views below or above the current viewport and moving active views to a recyclable pool as they move off-screencode while scrolling. This wayListView only needs to keep enough views in memory to fill its allocated space in the layout and some additional recyclable views—even when your adapter has hundreds of items. It will fill the space with rows in different ways—from top, from bottom, etc—depending on how the viewport changedcode. The image below visually summarizes what happens when you pan a ListViewdown.

With this framework in mind, let’s move on to the tips. As you’ve seen above,ListView dynamically inflates and recycles tons of views when scrolling so it’s key to make your adapter’s getView() as lightweight as possible. All tips resolve around making getView() faster in one way or another.

View recycling

Every time ListView needs to show a new row on screen, it will call thegetView() method from its adapter. As you know, getView() takes three arguments arguments: the row position, a convertView, and the parentViewGroup.

The convertView argument is essentially a “ScrapView” as described earlier. It will have a non-null value when ListView is asking you recycle the row layout. So, when convertView is not null, you should simply update its contents instead of inflating a new row layout. The getView() code in your adapter would look a bit like:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.your_layout, null);
    }

    TextView text = (TextView) convertView.findViewById(R.id.text);
    text.setText("Position " + position);

    return convertView;
}

View Holder pattern

Finding an inner view inside an inflated layout is among the most common operations in Android. This is usually done through a View method calledfindViewById(). This method will recursively go through the view tree looking for a child with a given IDcode. Using findViewById() on static UI layouts is totally fine but, as you’ve seen, ListView calls the adapter’s getView() very frequently when scrolling. findViewById() might perceivably hit scrolling performance in ListViews—especially if your row layout is non-trivial.

The View Holder pattern is about reducing the number of findViewById() calls in the adapter’s getView(). In practice, the View Holder is a lightweight inner class that holds direct references to all inner views from a row. You store it as a tag in the row’s view after inflating it. This way you’ll only have to usefindViewById() when you first create the layout. Here’s the previous code sample with View Holder pattern applied:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.your_layout, null);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);

        convertView.setTag(holder);
    } else {
        holder = convertView.getTag();
    }

    holder.text.setText("Position " + position);

    return convertView;
}

private static class ViewHolder {
    public TextView text;
}

Async loading

Very often Android apps show richer content in each ListView row such as images. Using drawable resources in your adapter’s getView() is usually fine as Android caches those internallycode. But you might want to show more dynamic content—coming from local disk or internet—such as thumbnails, profile pictures, etc. In that case, you probably don’t want to load them directly in your adapter’s getView() because, well, you should never ever block UI thread with IO. Doing so means that scrolling your ListView would look anything but smooth.

What you want to do is running all per-row IO or any heavy CPU-bound routine asynchronously in a separate thread. The trick here is to do that and still comply with ListView‘s recycling behaviour. For instance, if you run anAsyncTask to load a profile picture in the adapter’s getView(), the view you’re loading the image for might be recycled for another position before theAsyncTask finishes. So, you need a mechanism to know if the view hasn’t been recycled once you’re done with the async operation.

One simple way to achieve this is to attach some piece of information to the view that identifies which row is associated with it. Then you can check if the target row for the view is still the same when the async operation finishes. There are many ways of achieving this. Here is just a simplistic sketch of one way you could do it:

public View getView(int position, View convertView,
        ViewGroup parent) {
    ViewHolder holder;

    ...

    holder.position = position;

    new ThumbnailTask(position, holder)
            .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

    return convertView;
}

private static class ThumbnailTask extends AsyncTask {
    private int mPosition;
    private ViewHolder mHolder;

    public ThumbnailTask(int position, ViewHolder holder) {
        mPosition = position;
        mHolder = holder;
    }

    @Override
    protected Cursor doInBackground(Void... arg0) {
        // Download bitmap here
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (mHolder.position == mPosition) {
            mHolder.thumbnail.setImageBitmap(bitmap);
        }
    }
}

private static class ViewHolder {
    public ImageView thumbnail;
    public int position;
}

Interaction awareness

Asynchronously loading heavier assets for each row is an important step to towards a performant ListView. But if you blindly start an asynchronous operation on every getView() call while scrolling, you’d be wasting a lot of resources as most of the results would be discarded due to rows being recycled very often.

We need to add interaction awareness to your ListView adapter so that it doesn’t trigger any asynchronous operation per row after, say, a fling gesture on the ListView—which means that the scrolling is so fast that it doesn’t make sense to even start any asynchronous operation. Once scrolling stops, or is about to stop, is when you want to start actually showing the heavy content for each row.

I won’t post a code sample for this—as it involves too much code to post here—but the classic Shelves app by Romain Guy has a pretty good example. It basically triggers the async book cover loading once the GridView stops scrolling among other things. You can also balance interaction awareness with an in-memory cache so that you show cached content even while scrolling. You got the idea.

That’s all!

I strongly recommend watching Romain Guy and Adam Powell’s talk aboutListView as it covers a lot of the stuff I wrote about here. Have a look at Pattrnif you want to see these tips in action. There’s nothing new about the tips in this post but I thought it would be useful to document them all in one place. Hopefully, it will be a useful reference for hackers getting started on Android development.

Quick update. I’ve just announced Smoothie, a tiny library that that offers an easy way to do async loading in ListViews/GridViews. It incorporates most of the tips described in this blog post behind a very simple API. Have a look!

Got something to add? Find me on Twitter and Google+

内容概要:本文系统研究了电力系统短期负荷预测问题,提出并实现了基于极限学习机(ELM)及其智能优化改进模型的预测方法。研究涵盖标准ELM、白鲸优化算法(BWO)优化ELM和鹭鹰优化算法(IBOA)优化ELM三种模型,重点通过智能优化算法对ELM的输入权重与偏置参数进行全局寻优,有效克服了传统ELM因参数随机初始化导致的不稳定性和泛化能力不足的问题。文章完整呈现了从数据预处理、特征选择、模型构建、参数优化到预测结果对比分析的全流程,利用Matlab编程实现各模型的仿真验证,显著提升了预测精度与模型鲁棒性,为电力系统调度决策提供了可靠的技术支撑。; 适合人群:具备电力系统基础知识、时间序列预测理论及Matlab编程能力的高校研究生、科研机构研究人员以及电力公司从事负荷预测、电网调度与规划工作的技术人员。; 使用场景及目标:①应用于实际电力系统短期负荷预测业务中,提升电网运行调度的精细化与智能化水平;②作为智能优化算法与神经网络融合的经典案例,服务于学术论文撰写、科研项目申报及算法性能对比研究;③应对新能源大规模接入背景下负荷波动加剧的挑战,为构建高精度、强鲁棒性的现代负荷预测体系提供解决方案。; 阅读建议:建议读者结合所提供的Matlab代码进行动手实践,深入理解ELM网络结构与优化算法的集成机制,重点对比分析不同优化策略在收敛速度、预测误差(如MAE、RMSE、MAPE)等方面的性能差异,进而掌握智能优化技术在提升预测模型性能方面的关键作用。
内容概要:本文研究了基于Benders分解与输电网运营商(TSO)和配电网运营商(DSO)协调机制的不确定环境下输配电网双层优化模型,旨在提升高比例可再生能源接入背景下电网系统的协调性与鲁棒性。模型上层以系统整体经济性为目标进行优化调度,下层采用Benders分解实现TSO与DSO之间的信息交互与协同决策,通过引入割平面迭代机制保障求解的收敛性与全局最优性。研究充分考虑新能源出力与负荷需求的不确定性,构建了具有强适应性的双层优化框架,并基于Matlab完成了模型的编程实现与仿真验证,有效解决了多主体、多层级、多不确定性因素耦合下的电力系统优化调度难题。; 适合人群:具备电力系统分析、运筹学与优化理论基础,熟悉Matlab编程环境,从事智能电网、能源互联网、分布式能源集成、电力市场等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究高渗透率可再生能源条件下输配电网协同优化调度策略;②掌握Benders分解在电力系统双层优化建模中的应用方法与实现技巧;③构建TSO-DSO多主体协调机制,实现跨层级电网资源的高效互动与决策解耦;④提升对不确定性建模、分解算法设计及大规模优化问题求解能力。; 阅读建议:建议读者结合Matlab代码逐模块剖析模型构建流程,重点理解Benders割的生成逻辑、主从问题的信息传递机制及收敛判据设定,推荐在标准IEEE测试系统上复现实验以深入掌握模型特性与算法性能。
内容概要:本文提出了一种基于断线解环思想的配电网辐射状拓扑约束建模方法,旨在通过Matlab代码实现确保配电网在重构或运行过程中始终保持辐射状结构,防止环路形成,从而提升系统的安全性与稳定性。该方法通过系统性地识别网络中的潜在环路,并依据拓扑规则自动切断特定支路,有效处理配电网在优化调度、故障恢复及网络重构中的拓扑约束问题。文中详细阐述了算法的核心逻辑、数学模型构建过程、实现步骤及关键判据,并结合标准测试系统进行了仿真验证,充分证明了该方法在复杂配电网络中的有效性与实用性,尤其适用于含分布式电源接入的智能配电网场景。; 适合人群:具备一定电力系统分析基础和Matlab编程能力的高校研究生、科研人员,以及从事配电网自动化、智能电网优化、电力系统运行与控制等相关领域的工程技术人员。; 使用场景及目标:①解决配电网重构过程中的辐射状拓扑可行性验证与约束建模问题;②支撑含高比例分布式电源的配电网在故障恢复、动态重构中的安全运行分析;③为相关高水平EI期刊论文的模型复现、算法验证及科研项目申报提供可靠的代码实现与技术参考。; 阅读建议:建议读者结合Matlab代码与电力网络拓扑理论进行同步学习,重点理解断线解环的图论基础、环路搜索算法及支路断开逻辑的实现机制,并尝试在不同规模的测试系统(如IEEE 33节点系统)上进行仿真调试,以深入掌握该方法的应用技巧与优化潜力。
内容概要:本文围绕基于元模型优化算法的主从博弈多虚拟电厂动态定价与能量管理展开研究,提出了一种结合主从博弈理论与元模型优化方法的协同决策框架,通过Matlab代码实现,旨在解决高比例可再生能源接入背景下多虚拟电厂在复杂电力市场环境中的协调优化难题。研究构建了上层领导者(如主网或运营商)与下层跟随者(各虚拟电厂)之间的非对称互动模型,实现了动态电价制定与多主体能量调度的联合优化,有效提升了系统整体运行效率、经济收益与市场公平性。文中详细阐述了模型构建过程、算法设计思路及仿真验证方案,重点突出了元模型在降低计算复杂度、处理不确定性因素以及加速求解收敛方面的优势,具有较强的工程复现价值与理论参考意义。; 适合人群:具备一定电力系统运行、博弈论基础、优化建模能力及Matlab编程技能的研究生、科研人员,以及从事虚拟电厂运营、能源互联网规划、智能电网调度等相关领域的技术人员。; 使用场景及目标:①用于多主体能源系统中市场机制设计与竞价策略分析;②支撑含分布式能源的主动配电网协同优化调度研究;③为虚拟电厂参与电力市场的动态定价、需求响应与能量管理提供仿真验证平台与解决方案参考。; 阅读建议:建议读者结合Matlab代码逐模块理解算法实现流程,重点关注主从博弈架构的数学建模方式与元模型近似优化技巧的应用细节,同时可通过调整市场参数、负荷场景或可再生能源出力数据进行拓展性实验,以深化对模型鲁棒性与泛化能力的理解。
内容概要:本文围绕列车-轨道-桥梁耦合系统开展动力学交互仿真研究,基于Matlab平台构建多体动力学数值模型,综合考虑列车移动荷载、轨道结构特性与桥梁动态响应之间的耦合作用,实现对列车通过桥梁过程中振动传递规律、结构受力特性和动力响应行为的精确模拟。研究涵盖系统建模、运动方程求解、关键参数设定及仿真结果分析全过程,提供完整的Matlab代码实现方案,有助于深入理解轨道交通基础设施在运营条件下的动力性能,为桥梁结构安全性评估、轨道平顺性优化及减振设计提供理论支持和技术手段。; 适合人群:具备一定结构动力学、振动力学基础知识及Matlab编程能力的研究生、高校教师、科研机构研究人员以及从事铁路与桥梁工程设计、运维的工程技术人才。; 使用场景及目标:①用于高速铁路桥梁在列车荷载作用下的动力响应仿真与安全评估;②支撑轨道-桥梁系统减振降噪设计与结构优化;③作为高等教学与科研中的典型案例,辅助讲授多体系统动力学建模与数值仿真方法; 阅读建议:建议读者结合结构动力学相关理论教材,逐步运行并调试所提供的Matlab代码,重点关注质量-刚度-阻尼矩阵的构建、轮轨接触关系处理、时间积分算法实现等核心模块,深入理解仿真结果的物理含义及其工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值