ListView.hasWindowFocus==true but child views hasWindowFocus==false

后端 未结 1 1276
忘了有多久
忘了有多久 2021-01-26 09:35

Sometimes I notice that, for a View v

 - v.hasWindowFocus()==false
 - ((View)v.getParent()).hasWindowFocus())==true

If I understand the docs c

相关标签:
1条回答
  • 2021-01-26 10:21

    The main reason is that ListView doesn't like an adapter having an array of views.

    So the problem is triggered by code like

    public View getView (int position, View convertView, ViewGroup parent)
    {
        return _views[position];
    }
    

    When looking at the ListView code (or rather it's parents AbsListView.obtainView method) you'll see code like

        if (scrapView != null) {
            ...
            child = mAdapter.getView(position, scrapView, this);
            ...
            if (child != scrapView) {
                mRecycler.addScrapView(scrapView);
    

    It can happen that getView(position,...) is called with scrapView != _views[position] and hence scrapView will be recycled. On the other hand, it is quite likely that the same view is also added again to ListView, resulting in views getting weird states.

    This should be fixed in ListView IMO.

    0 讨论(0)
提交回复
热议问题