如何在getView()方法的创建自己的自定义适配器的时候?自己的、自定义、适配器、时候

2023-09-12 06:51:23 作者:大师兄摘的就是好吃i

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

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

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

为寻找谢谢了!

推荐答案

1: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.