2

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);

frankish
  • 6,738
  • 9
  • 49
  • 100
  • What do you mean by "not well" and "smoothly"? – Tim Apr 24 '13 at 19:56
  • Show us at least the code where you create the view for the ListView item. – SimonSays Apr 24 '13 at 19:57
  • 1
    Use Traceview to determine precisely where you are spending your time. Also, ativate `StrictMode` to yelp at you regarding obvious problems on the main application thread. – CommonsWare Apr 24 '13 at 20:09
  • For better smooth scrolling use [ViewHolder](http://developer.android.com/training/improving-layouts/smooth-scrolling.html) and Background thread. You may like to check [this](http://stackoverflow.com/questions/3208897/android-listview-viewholder-when-to-use-it-and-when-not-to/17485652#comment30104745_17485652) one. – swiftBoy Nov 25 '13 at 12:20

3 Answers3

9

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)

And also this ViewHolder implementation example/library

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

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
4
  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

Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
2

You should use the View Holder pattern, also test if the scrolling is slow without the debugger connected.

evertvandenbruel
  • 1,133
  • 8
  • 13