ListView disappears before updating itself when I pull to refresh

Viewed 474

I use a ListView with a PullToRefresh mechanism using SwipeRefreshLayout. I add the SwipeRefreshLayout dynamically in code to the ListView when I initialize.

SwipeRefreshLayout swipeRefreshLayout = new SwipeRefreshLayout(listView.getContext());

if (listView.getParent() != null) {
    final ViewGroup parent = (ViewGroup) listView.getParent();
    final int index = parent.indexOfChild(listView);
    parent.removeViewAt(index);
    parent.addView(swipeRefreshLayout, index);
    listView.setVisibility(View.VISIBLE);
}

swipeRefreshLayout.addView(listView);
swipeRefreshLayout.setOnRefreshListener(...) { }

The above code adds the swipeRefreshLayout to the parent (at the position that the listView is currently in), and then adds the listView as the child of the swipeRefreshLayout.

When I pull to refresh, a network call is made - data is fetched, stored in the DB, and loaded using a CursorAdapter.

The problem :

When I pull to refresh, I see a blank screen for a second and the UI updates itself with all the items. But when the blank screen is showing and I touch the screen, it responds to click events and the ListView immediately shows.

I need the ListView to display all the time while data is being fetched (while the PTR view is showing) and update immediately when new data is available.

Is there something I'm doing wrong? I have tried running setRefreshing to true by posting a runnable to the SwipeRefreshLayout and fetching new data. I don't think this has got anything to do with the data fetch in particular.

The adapter is not being changed while the blank screen appears (which means that the ListView should still be visible).

Any ideas?

1 Answers
Related