Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize Android ListView?

I have customized my ListAdapter and i show 3 different Images (items) in 1 row. It works perfectly (according to it's function). However, it's not possible to scroll the ListView smoothly.

I am using setBackgroundImage on ImageViews and i use an HashMap to cache resourceId; so i don't have to use

resId=getContext().getResources().getIdentifier(resName, "drawable",appContext.getPackageName());

again and again.

I think i am missing something as the ListView is not scrolling well. Also if i try it on a tablet where my code automatically fills more than 3 items on a row, tablet's listview is almost unscrollable.

What am i doing wrong here?

UPDATE:

I create ListView programmatically in my Flags (country flags) Activity's onCreate method:

root=(ViewGroup)this.findViewById(R.id.root);

    listView=new ListView(this);
    listView.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));

    /*
    ArrayList<Country> dataList=new ArrayList<Country>(){{
        add(new Country("jp"));
        add(new Country("de"));
        add(new Country("it"));
    }};*/

    CountryListAdapter countryListAdapter=new CountryListAdapter(this);


    countryListAdapter.setDataSource(allCountries);


    listView.setAdapter(regionListAdapter);
    listView.setBackgroundColor(0xFF000000);
    listView.setDividerHeight(0);

    root.addView(listView);

like image 375
frankish Avatar asked Dec 18 '25 19:12

frankish


2 Answers

Study, study and study ;-)

And my tip is to use ViewHolder pattern, for large number of item's of same layout (even if it is the simpliest one, such as single TextView)

  • Google I/O 2009 - ...Make your Android UI Fast and Efficient http://www.youtube.com/watch?v=N6YdwzAvwOA
  • Google I/O 2010 - The world of ListView http://www.youtube.com/watch?v=wDBM6wVEO70
  • http://www.technotalkative.com/category/android/listview/
  • ViewHolder - good practice
  • why does the ViewHolder pattern work?
  • Is it crucial for performance to have ViewHolder as static in a ViewHolder pattern?

And also this ViewHolder implementation example/library

  • https://github.com/rtyley/android-viewholder-listviews (Apache 2.0)

Also if you do use images in your ListView items layout, you can use some libraries to download images asynchronously, such as:

  • https://github.com/nostra13/Android-Universal-Image-Loader
  • Lazy load of images in ListView
  • http://android-developers.blogspot.cz/2010/07/multithreading-for-performance.html
like image 118
Marek Sebera Avatar answered Dec 20 '25 12:12

Marek Sebera


  1. Use static ViewHolder pattern

  2. Inflating layout is expensive task so avoid inflating layout as much as possible
    Replace

    LayoutInflater mInflater = LayoutInflater.from(context);
    convertView = mInflater.inflate(R.layout.listview_item, null);
    

    With

    if (convertView == null) {
    
        LayoutInflater mInflater = LayoutInflater.from(context);
        convertView = mInflater.inflate(R.layout.listview_item, null);
    }
    
  3. Do image loading task in seperate thread so getView only focus on View drawing

  4. add <item name="android:windowNoTitle">true</item> in your activity theme

  5. add property android:animationCache="false" in ListView

  6. add property android:scrollingCache="false" in ListView

like image 24
Kirit Vaghela Avatar answered Dec 20 '25 13:12

Kirit Vaghela



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!