What is the purpose of `convertView` in ListView adapter?

前端 未结 4 1965
逝去的感伤
逝去的感伤 2020-12-08 07:58

In android, I usually use MyAdapter extends ArrayAdapter to create view for the ListView, and as a result, I have to override the function

相关标签:
4条回答
  • 2020-12-08 08:10

    From the documentation,

    convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

    In other words, this parameter is used strictly to increase the performance of your Adapter. When a ListView uses an Adapter to fill its rows with Views, the adapter populates each list item with a View object by calling getView() on each row. The Adapter uses the convertView as a way of recycling old View objects that are no longer being used. In this way, the ListView can send the Adapter old, "recycled" view objects that are no longer being displayed instead of instantiating an entirely new object each time the Adapter wants to display a new list item. This is the purpose of the convertView parameter.

    0 讨论(0)
  • 2020-12-08 08:14

    convertView is used to reuse old view.

    Please understand Adapter functionality in android. Adapter enables you to reuse some view with new data.

    So if a list is of 15 items, but window can show only 5 items, then at first convertView would be null, and we need to create new views for these five items, but when you scroll down, you have two options, either create 6-10 views, or re-use old views and load new data into these views. Adapter and convertView enables you to do the later method.

    0 讨论(0)
  • 2020-12-08 08:23

    convertView is the ListView Item Cache that is not visible, and hence it can be reused. It lets the ListView need not create a lot of ListItems, hence saving memeory and making the ListView more smooth.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView == null) {
            view = this.inflator.inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        else {
            view = convertView;
        }
        // remaining implementation
        return view;
    }
    
    0 讨论(0)
  • 2020-12-08 08:29

    Shorter Version :

    Please read @Alex Lockwood's and @jeet's answer.

    My Answer :

    Before why, which is the better/proper way of using convertView in getView()? Well explained by Romain Guy in this video.

    An example,

    @Override
     public View getView(final int position, View convertView, ViewGroup parent)            {
       View rowView = convertView;
       ViewHolder holderObject;
    
     if (rowView == null) {
         rowView = inflater.inflate(R.layout.list_single_post_or_comment, parent, false);
         holderObject = new HolderForContent();
         mapHolder(holderObject, rowView);
         rowView.setTag(holderObject);
       } else {
         holderObject = (HolderForContent) convertView.getTag();
       }
       setHolderValues(holderObject, position);
       return rowView;
     }
     
     private class ViewHolder {
        TextView mTextView;
     }
     
     mapHolder(holderObject, rowView) {
        //assume R.id.mTextView exists
        holderObject.mTextView = rowView.findViewById(R.id.mTextView);
     }
     
     setHolderValues(holderObject, position) {
        //assume this arrayList exists
        String mString = arrayList.get(position).mTextViewContent;
        holderObject.mTextView.setText(mString);
     }
    

    Above is just an example, you can follow any type of pattern. But remember this,

    @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
     
       if (convertView == null) {
            // todo : inflate rowView. Map view in xml.
       } else {
            // todo : get already defined view references
       }
    
       // todo : set data to display
     
     return rowView;
     
     }
    

    Now coming to purpose of convertView. The why?

    convertView is used for performance optimization [see chart in slide 14 by Romain Guy] by not recreating view that was created already.

    Sources : Any corrections are welcome. I actually gathered this info through these links,

    Read about getView() in Android developer documentation.

    Romain Guy speaking about getView() in video "Turbo Charge Your UI" at Google IO 2009 and material used for presentation.

    A wonderful blog by Lucas Rocha.

    Those who want to take a deep dive into source code : ListView and a sample implementation of getView() can be seen in source code for arrayAdapter.

    Similar SO posts.

    what-is-convertview-parameter-in-arrayadapter-getview-method

    how-do-i-choose-convertview-to-reuse

    how-does-the-getview-method-work-when-creating-your-own-custom-adapter

    0 讨论(0)
提交回复
热议问题