Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement pull to refresh on a ListFragment

I'm trying implement "pull to refresh" on a ListFragment but right now none of the drop in libraries seem to support it. There's no way to detect overscroll on the list fragment that I can see so I'm wondering if anyone has found a means to get this working?

--

Using Christian's tip I used the following for my onCreateView() method.

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
   PullToRefreshListView listView = new PullToRefreshListView(getActivity());
   mAdapter = new HomeTweetListAdapter(getActivity(), R.layout.tweet_list_item, tweets);
   listView.setAdapter(mAdapter);
   return listView;
}

Like Christian said you can only do this with a Fragment. Returning anything other than a ListView on a ListFragment errors out.

EDIT: To clarify I am using Johan's PullToRefresh library

like image 419
George Avatar asked Dec 27 '11 18:12

George


People also ask

How to implement Swipe to refresh in Android?

To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView . Remember that SwipeRefreshLayout only supports a single ListView or GridView child. You can also use the SwipeRefreshLayout widget with a ListFragment .

How to add refresh button in Android?

You should add the refresh action as a menu item, rather than as a button, by setting the attribute android:showAsAction=never . If you display the action as a button, users may assume that the refresh button action is different from the swipe-to-refresh action.

How to refresh Android ListView?

To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView. Also note that the method notifyDataSetChanged() has to be called on UI thread.


1 Answers

I actually make it work using fragments (not ListFragment). So it is basically the same, just return the PullToRefreshListView from your onCreateView method and that's it. It should also work with ListFragment; remember that you must return a ListView from onCreateView if you use ListFragment (you can return whatever you want if you use just Fragment).

like image 156
Cristian Avatar answered Oct 23 '22 00:10

Cristian