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

。_饼干妹妹 提交于 2019-12-04 06:30:04

问题


Sometimes I notice that, for a View v

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

If I understand the docs correctly, this should never be the case.

v.getParent() is a (subclass) of ListView

Any ideas on what might be causing this?

Note: I assume this hasWindowFocus-mismatch is the root cause for this issue


回答1:


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.



来源:https://stackoverflow.com/questions/11257357/listview-haswindowfocus-true-but-child-views-haswindowfocus-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!