什么在getView究竟发生发生、getView

2023-09-06 17:46:43 作者:初闻不知曲中意

我看到很多getView的适配器扩展覆盖:

I see this a lot in Adapter extensions override of getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.whatever, null);
        }

我得到语义上发生了什么 - 如果convertView为null,则它充气,但我真的不明白,为什么? - 什么情况下会convertView为空,在什么情况下将它已经是一个视图

I get semantically what's happening - "if convertView is null then inflate it", but I don't really understand why - what circumstance would convertView be null, and in what circumstance would it already be a View?

同时,(我知道每个职位1个问题)的,但由于涉及到上述的 - 什么充气法中的究竟发生?我知道它一般不(膨胀期资源进行分析和填充),但不完全神交它...

Also (and I know "1 question per post"), but as it relates to the above - what's exactly happens during the inflate method? I know what it does generally ("inflates" a view resource to be parsed and populated), but don't fully grok it...

TYIA

推荐答案

假设你的列表适配器有100​​0个对象。每个对象再由视图psented $ P $。在手机屏幕有地方仅为也许有些10这样的观点。您滚动希望看到更多的项目清单。有些项目走出去的视线,因为你滚动。这是没有意义的框架,以创造更多的意见,因为他们将是完全一样的那些刚才已经淡出人们的视线。因此,框架可以重复使用它之前创建的一些看法,所以它通过为您提供您提供了这样的可能性,一个非空 convertView

Say your list adapter has 1000 objects. Each object is represented by a view. On the phone screen there's place only for maybe some 10 such views. You scroll the list wanting to see more items. Some items go out of sight because you're scrolling. It makes no sense for the Framework to create more views as they will be exactly the same as those which have just gone out of sight. Framework thus can reuse some views it created before and so it offers to you such a possibility by offering you a non-null convertView.

在充气的方法View对象被创建了一些XML资源。 XML资源具有足够的对于查看视图的描述要创建等等膨胀方法做的创造。因此,没有视图 - 创建通过虚报一个新的,有一种观点已经 - 你可以重复使用它(你没有,但你应该)

During the inflate method a View object gets created out of some XML resource. The XML resource has a description of a View sufficient for that View to be created and so the inflate method does that creation. So no view - you create a new one by inflating, there is a view already - you can reuse it (you don't have to but you should)