Android的列表视图筛选与复育 [英] Android Listview Filtering versus Repopulation

查看:113
本文介绍了Android的列表视图筛选与复育的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些建议真的。我的应用程序填充使用mediastore光标负荷的列表视图。这是拉音乐链接到用户定义的文件夹,在大多数情况下将其所有存储的音乐。我有一个在其上使用爱可视平板电脑拥有大约10000首歌曲,运行Android 2.2一个β测试。虽然对于大多数用户来说性能pretty滑头,我想改善用户像这样的经历。

after some advice really. My app fills a list view on load using a mediastore cursor. This is pulling music linked to user defined folder, which in most cases will be all of their stored music. I have one beta tester that is using an Archos Tablet with approximately 10000 songs on it, running android 2.2. While performance for most users is pretty slick, I wanted to improve the experience for users such as this.

目前的流程:

用户加载应用程序。
应用发现默认的文件夹
应用填充列表视图与范围内的音乐和文件夹下
用户移动到文件夹进一步下跌的树,列表视图是根据所选文件夹重新填充
用户再次移动....列表基于所选文件夹重新填充...

User loads app. App finds default folder App populates list view with music within and below that folder User moves to a folder further down the tree, list view is repopulated based on the selected folder User moves again....list is repopulated based on the selected folder...

所以,我想知道是这样的 - 它是更快/更有效地使用以下过程:
用户负载的应用程序
应用发现默认的文件夹
应用填充列表视图与范围内的音乐和文件夹下
用户移动到文件夹在树中,列表将筛选到该文件夹
如果用户向上树比默认数据(即新文件电位)移动越高,列表视图重新填充,但只有在这种情况下

So what I'm wondering is this - is it faster/more efficient to use the following process: User loads app App finds default folder app populates list view with music within and below that folder user moves to a folder within the tree, THE LIST IS FILTERED TO THAT FOLDER if the user moves higher up the tree than the default data (i.e. potential for new files), the list view is repopulated, but only in this circumstance.

所以基本上,我的问题是如何筛选比较复育?

So basically,my questions is "how does filtering compare to repopulation?"

推荐答案

一个非常好的问题。让我尝试回答这个问题。

A very good question. Let me try to answer this.

过滤实际上是复育的的ListView ,而创建/获得一个新的集合,并告诉适配器它的内容通过调用 notifyDataSetChanged 改变。

Filtering is actually repopulation the ListView, whereas you create/get a new collection and tell the Adapter it's content has changed by calling notifyDataSetChanged.

对于ListView中重的工作是在它 getView 呼叫的适配器。我这个测试自己,如果你每天getView调用时充气一个新的View,性能下降。天工。

The 'heavy' work for a listView is that getView call in it's adapter. I've tested this myself, and if you inflate a new View every time getView is called, the performance drops. Heavenly.

ListView的适配器构建,使得已经膨胀视图可以被重新使用,从而能够解决上述命名问题。此外,唯一可见的意见被加载,所以它不是像适配器打算,如果你告诉它的收藏是10000项大创造10000的看法。

The ListView's adapter is built so that already inflated views can be re-used, which tackles above named problem. Besides, only visible views are loaded, so it's not like the Adapter is going to create 10000 views if you tell it's collection is 10000 items big.

notifyDataSetChanged 将告诉适配器重建列表视图内容,但它仍包含previously膨胀的看法。因此,这里是一个大的性能胜利。

notifyDataSetChanged will tell the adapter to rebuild the listviews content, but it still contains previously inflated views. So here is a big performance win.

所以,我对你的建议是,如果你使用的是相同的行布局只是重新填充的ListView 使用 notifyDataSetChanged 。我这个多次实施自己没有注意到任何UI性能问题。只要确保你做一个收集后台线程的过滤。 (的AsyncTask 说到这里派上用场)。

So my advice for you is, when you are using the same 'row layout' to just repopulate the ListView using notifyDataSetChanged. I've implemented this multiple times myself without noticing any UI performance issues. Just make sure to do the filtering of your collection an a background thread. (AsyncTask comes in handy here).

最后一个提示:你有没有手机,多数民众赞成很老?或者你认识的人呢?查找最慢的手机,您可以和它测试应用程序性能。我有一个的HTC Legend自己,这是过时的和缓慢的,如果他妈的,但完美的性能测试。如果它运行我的(旧)的手机,它运行在任何手机上。

One last tip: Do you have any phone thats quite old? Or someone you know does? Find the slowest phone you can and test your application on it for performance. I have a HTC Legend myself, which is outdated and slow if f*ck, but perfect for performance testing. If it runs on my (old) phone, it runs on any phone.

伪code样本,如果你的应用程序流:

Pseudo code sample if your applications flow:

public class FolderListActivity extends Activity implements OnItemSelected {

    // NOTE: THIS IS PSEUDO CODE

    private ListView listView
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstaceState);

        // setContentView here 
        listView = (ListView)findViewById(R.id.your_listview_here);
        listView.setOnItemSelectedListener(this);
    }

    public class AsyncLoadMusicLocationTask extends AsyncTask<Void, Void, List<String>> {

        public List<String> doInBackground(Void... params) {
            // Load the information here, this happens in the background
            // using that cursor, i'm not sure what kind of things you are using
            // So I assumed a List of Strings
        }

        @Override
        public void onPostExecute(List<String> result) {
            // Here we have our collection that was retrieved in a background thread
            // This is on the UI thread

            // Create the listviews adapter here
            adapter = new Adapter(result, and other parameters);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void onItemSelect(Some params, not sure which) {

        // THIS SHOULD BE DONE ON THE BACKGROUND THE PREVENT UI PERFORMANCE ISSUES

        List<String> collection = adapter.getObjects();
        for (int i = 0; i < collection.size(); i++) {
            // Filter here
        }

// this method will most probably not exist, so you will need to implement your own Adapter class
        adapter.setObjects(collections);
        adapter.notifyDataSetChanged();
    }
}

这篇关于Android的列表视图筛选与复育的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆