0

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

2
  • apparently, onWindowVisibilityChanged gets called for these Views despite that they are part of ListView Jun 29, 2012 at 10:20
  • I use a custom adapter containing an array of Views. Within getView(position, ...), I return the View at the specified position. Apparently, ListView doesn't like this and sometimes recycles views that shouldn't be recycled. Jun 29, 2012 at 10:58

1 Answer 1

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.

3
  • This is really helpful, thank you. The reason I am using an array of views is that I want to be able to look ahead and prepare the views before they are passed to the adapter for performance reasons. Obviously I can load the bitmaps without putting them in the view, so I can still do that much look-ahead processing, but how would you go about the other stuff—given that you should presumably be making views on the fly (from those passed to getView(...) or such)? Nov 6, 2012 at 18:27
  • 1
    typically you write something like View ret = convertView; if(ret==null) {ret = //create view; probably an inflate from XML but canalso be done in Java}; //(re-)init the view. It's important to have the (re)init outside the if test since it should also be done when convertView!=null Nov 6, 2012 at 19:24
  • That's what I thought; I vaguely remembered having the same problem when I was doing it this way on another project, though. Anyway, I will try it out and see what happens—thanks! Nov 7, 2012 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.