android - 如何理解BaseAdapter.getVeiw()参数convertView的null与非null
PHP中文网
PHP中文网 2017-04-17 14:32:25
0
3
903
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageview;
    if (convertView==null){
        imageview=new ImageView(MyActivity.this);
        imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageview.setPadding(5,0,5,0);

    }else{
        imageview=(ImageView)convertView;
    }

    imageview.setImageResource(imageId[position]);
    return imageview;
}

当一张图片滑进屏幕的时候,调用这个getview()?那么其中第一个if条件null是什么?如何理解这里面的条件语句?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
巴扎黑

To put it simply, it is for reuse and avoids generating a new view from the layout resource file (or generating a new view through code) every time .
For example, like ListView or GridView, if N items can be displayed on the screen, then getView will be called N times to provide a view of the corresponding position. Reusing previously generated views can improve efficiency.

Parameter description of the getView() method in the android.widget.Adapter source code file:

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

But you still need to check if it is empty and of the appropriate type.

洪涛

This is to avoid creating duplicate objects. It is for better reuse

Ty80

getView creates a view object through the adapter to fill in the listview, etc. to determine whether the convertview is cached. If it is cached, there is no need to create a new view and the purpose of reuse can be achieved. Nowadays, the getview method is generally not written like this. There is a more efficient way to write it. It has been too long since I wrote about Android

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!