0

I'd like to create less work for the getView method of an android list Adapter. I have been reading that we can let a ViewGroup update the view instead of the getView method and thus make scrolling faster. I need some help understanding this concept and here is what i have so far:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewGroup myViewGroup;
    if (convertView == null) {
        myViewGroup = (MyViewGroup) LayoutInflater.from(context)
          .inflate(R.layout.banana_phone, parent, false);
    } else {
        myViewGroup = (MyViewGroup) convertView;
    }

    InfoObject info = getItem(position);

    myViewGroup.update(info); //how does this part work ? this is synchrounous so getView has to wait, doesn't it ?

    return myViewGroup;
}

I got this idea from this site

So my issue is how do i make the viewGroup update without letting getView wait. I believe in the implementation i have above getView would wait until the viewgroup is updated, correct ? so this would slow down the listview still. I would like an example of what the custom viewgroup would look like (or non custom if thats the case)?

could anyone give an example what MyViewGroup would look like after extending ViewGroup and say updating a textview in the listview as im not understanding how to structure the custom viewgroup so that it updates a textview for example?

Community
  • 1
  • 1
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • *viewGroup update without letting getView wait* ... it is complicated ... *correct ?* correct ... remeber that this code is reusing the views ... so if you let *.update* to be "delayed" somehow you can face the problem that the data is outdated (myViewGroup should be updated in another InfoObject) ... the good example how it can be implemented is hidden inside image loader's code ... – Selvin Jan 19 '15 at 22:57
  • 2
    It looks like he's just putting all the updating in to the ViewGroup that would normally be in "getView()". It's not speeding up or slowing down the ListView. It's just moving the work from one class to another. – DeeV Jan 19 '15 at 23:17
  • 2
    Basically the adapter only has to know about MyViewGroup. It doesn't have to know what View elements are inside MyViewGroup so it's easier to make changes. – DeeV Jan 19 '15 at 23:18
  • could you give an example what MyViewGroup would look like after extending ViewGroup and say updating a textView? – j2emanue Jan 20 '15 at 00:44

1 Answers1

0

You are correct, the update call is (and should be!) synchronous, and getView will have to wait.

Creating your own ViewGroup for a list provides better decoupling - it has no performance benefits, but rather moves the setting of values within the view into a separate class to the adapter. If you then used this view in another layout, say as a header in a detail view, you would reduce code duplication by utilizing the update method.

Adam S
  • 16,144
  • 6
  • 54
  • 81
  • 1
    I think i get it now. this way the viewgroup could be reused on different list if it had teh same type of elements. This is excellent. DeeV also had the right idea +1. – j2emanue Jan 20 '15 at 01:23