Recycling views in custom array adapter: how exactly is it handled?

后端 未结 5 879
栀梦
栀梦 2020-12-15 11:15

I am having an unclear issue concerning the recycling of views in a getView method of a custom array adapter.

I understand that elements are reused, but how do I kn

5条回答
  •  無奈伤痛
    2020-12-15 11:51

    I understand that elements are reused, but how do I know exact what to implement in the first part of the if statement, and what in the second?

    The organization is quite simple once you get the hang of it:

    public View getView(int position, View convertView, ViewGroup parent) {
    
        if (convertView == null) {
            /* This is where you initialize new rows, by:
             *  - Inflating the layout,
             *  - Instantiating the ViewHolder,
             *  - And defining any characteristics that are consistent for every row */
        } else {
            /* Fetch data already in the row layout, 
             *    primarily you only use this to get a copy of the ViewHolder */
        }
    
        /* Set the data that changes in each row, like `title` and `size`
         *    This is where you give rows there unique values. */
    
        return convertView;
    }
    

    For detailed explanations of how ListView's RecycleBin works and why ViewHolders are important watch Turbo Charge your UI, a Google I/O presentation by Android's lead ListView programmers.

提交回复
热议问题