12

When inflating views for a listView, is it better to have all textViews without android:text in the .xml file and how much does that affects the speed? What about ViewStubs, would that be even faster?

When inflating a LinearLayout with 8 textViews without android:text and with android:text="@string/abc", does that change anything ? note that i am reusing views, so maybe only 10 get inflated and then reused i don't know.

I am developing on a ZTE Blade, so that'a single 600Mhz CPU and not a quad core ...

Ron
  • 24,175
  • 8
  • 56
  • 97
max4ever
  • 11,909
  • 13
  • 77
  • 115
  • 1
    if you make your row's height bigger, android has to draw less lines and is FASTER, also you can justify by saying people with big fingers can select more easily the rows :) – max4ever Aug 08 '12 at 09:23

3 Answers3

8

You can try this, not sure if it will speed up, but give it a shot.

Inflate the layout in a background thread.

View getView(int position, View convertView, ...) {
    View v;
    if (convertView == null) {
        Start a background thread to inflate your linearLayout. 
        Pass item data and view 'v' to it.

        v = inflate a simple dummy textview;
        return v;
    }
    set normal stuff to convertview here.
    return convertView ;
 }

In the background thread,

  • Inflate your Linearlayout into 'v'
  • Set all the data.
  • Then invalidate the view v.

    v.postInvalidate();
    
Ron
  • 24,175
  • 8
  • 56
  • 97
  • i can't get it to work, v.postInvalidate() doesn't do anything – max4ever Aug 03 '12 at 15:37
  • the idea is good, but i can't get it to work, do you have an example of simple working code somewhere? – max4ever Aug 03 '12 at 15:55
  • how do you swap convertView with v, there is no way to do it – max4ever Aug 03 '12 at 16:01
  • the idea is to inflate a linearlayout and then add to it all the elements one wants, this way it works – max4ever Aug 03 '12 at 16:11
  • Can you please add the final working code to your post!!... Does it speed up? – Ron Aug 03 '12 at 16:27
  • it sort of speeds up very little noticeable, i guess the only way to have the speed is to listen to RomainGuy and build your own custom View just like they did with gmail application – max4ever Aug 03 '12 at 17:01
  • this method of loading is significantly effective if the item layout is nested deep.. Your case, the layout is quite simple. Anyways This post would be helpful to some one else.. – Ron Aug 03 '12 at 17:29
  • One other thing you can do is to create all views in java.. not inflate from xml.. – Ron Aug 03 '12 at 17:40
3

This is quite helpful when speeding up list views, especially the View Holder bit: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

I've got a ZTE Blade too, they're slow, but apps that run well on it run amazing on 'normal' phones :)

Todd Davies
  • 5,484
  • 8
  • 47
  • 71
  • yes I'm using View Holder, I was wondering whether the inflate had any influence – max4ever Jul 26 '12 at 15:36
  • I don't think it does, but if your row items have lots of widgets then it'll slow everything a little. Are you trying to improve the scrolling speed? – Todd Davies Jul 26 '12 at 15:49
  • even though i use an adapter my app hangs for some seconds after Ajax return and I fill the list with 40 rows, and i'm using a samsung galaxy tab, can't figure how to optimize more – max4ever Jul 26 '12 at 16:29
  • I think that you'll be accessing the internet in the UI thread by the sound of it. See here for an example: http://labs.makemachine.net/2010/05/android-asynctask-example/ – Todd Davies Jul 26 '12 at 19:46
  • no no i'm using asynctask, just that when i add 40rows at once it lags , was thinking of adding one by one with some delay in beetween, dunno – max4ever Jul 27 '12 at 07:51
  • Post your code in your question, you may be doing something not quite right that makes it slow, AsyncTasks are hard to get right sometimes :P – Todd Davies Jul 27 '12 at 08:06
  • it was a theoretical questions of how much that has an effect on the speed, i don't need code to use an async – max4ever Jul 27 '12 at 13:57
  • I see, if you're not using multiple threads then I would definitely recommend it, good luck! – Todd Davies Jul 27 '12 at 13:59
3

If you are replacing the text in the textviews anyway, then you shouldn't bother populating them within the XML.

A real killer for performance that is tempting to do within the simple listviews is nested linearlayouts. With a bit of work any linearlayout tree could be replaced with a relativelayout.

Make sure you are not using findViewById outside of the if (convertView == null) { block either by using ViewHolder style or simply setting in the view.setTag(R.id.resId, findViewById(R.id.resID))

You mentioned in comments that you are experiencing a delay when returning from your asynctask . Make sure you aren't reloading the table after each entry. Do a bulk update of the data source and then reload the table.

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32