Search code examples
androidandroid-listviewandroid-memoryandroid-viewgroup

Android adapter getView better decoupling with a viewGroup


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?


Solution

  • 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.