如何在getView()方法的创建自己的自定义适配器的时候? [英] How does the getView() method work when creating your own custom adapter?

查看:100
本文介绍了如何在getView()方法的创建自己的自定义适配器的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试阅读大量的文章,但我仍然无法理解。我的具体问题是:

I have tried reading numerous articles but I still can't understand. My specific questions are:

  1. 什么是完全LayoutInflater?
  2. 的功能
  3. 为什么所有的,我读过检查convertview为空或不首先的文章?这是什么意思,当它为空,什么意思时,是不是?
  4. 什么是家长parameted,这种方法接受?

为寻找谢谢了!

推荐答案

1:<一href="http://developer.android.com/reference/android/view/LayoutInflater.html"><$c$c>LayoutInflater需要你的布局XML的文件,并从它的内容创建不同的视图对象。

1: The LayoutInflater takes your layout XML-files and creates different View-objects from its contents.

2:适配器在建造时,重复使用次数,当查看滚动,这样不再可见,它可以被用于显示新的景色之一。这种重复使用View是 convertView 。如果这是null,则意味着没有回收的查看,我们必须建立一个新的,否则我们应该用它来避免建立新的。

2: The adapters are built to reuse Views, when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing. This reused View is the convertView. If this is null it means that there is no recycled View and we have to create a new one, otherwise we should use it to avoid creating a new.

3:在提供这样你就可以吹你的看法到,对于合理布置参数

3: The parent is provided so you can inflate your view into that for proper layout parameters.

所有这些结合在一起,可以用来有效地创建将出现在您的列表(即需要一个适配器或其他视图)的观点:

All these together can be used to effectively create the view that will appear in your list (or other view that takes an adapter):

public View getView (int position, View convertView, ViewGroup parent){
    if( convertView == null ){
        //We must create a View:
        convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }
    //Here we can do changes to the convertView, such as set a text on a TextView 
    //or an image on an ImageView.
    return convertView;
}

注意使用 LayoutInflater ,即可以被用作论据,以及如何 convertView 被重复使用。

Notice the use of the LayoutInflater, that parent can be used as an argument for it, and how convertView is reused.

这篇关于如何在getView()方法的创建自己的自定义适配器的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆