Last active
August 29, 2015 14:03
-
-
Save JeffInMadison/9cba721e3e72bf7cac12 to your computer and use it in GitHub Desktop.
SwipeRefreshListFragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.bim.android.ui.custom; | |
| import android.app.ListFragment; | |
| import android.content.Context; | |
| import android.os.Bundle; | |
| import android.support.v4.view.ViewCompat; | |
| import android.support.v4.widget.SwipeRefreshLayout; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.ListView; | |
| /** | |
| * Created by Jeff on 6/30/14. | |
| * Taken from Android Samples SwipeRefreshListFragment | |
| * https://developer.android.com/samples/SwipeRefreshListFragment/src/com.example.android.swiperefreshlistfragment/SwipeRefreshListFragment.html | |
| */ | |
| public class SwipeRefreshListFragment extends ListFragment { | |
| @SuppressWarnings("UnusedDeclaration") | |
| private static final String TAG = SwipeRefreshListFragment.class.getSimpleName(); | |
| private SwipeRefreshLayout mSwipeRefreshLayout; | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
| // Create the list fragment's content view by calling the super method | |
| final View listFragmentView = super.onCreateView(inflater, container, savedInstanceState); | |
| // Now create a SwipeRefreshLayout to wrap the fragment's content view | |
| mSwipeRefreshLayout = new ListFragmentSwipeRefreshLayout(container.getContext()); | |
| // Add the list fragment's content view to the SwipeRefreshLayout, making sure that it fills | |
| // the SwipeRefreshLayout | |
| assert listFragmentView != null; | |
| mSwipeRefreshLayout.addView(listFragmentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
| // Make sure that the SwipeRefreshLayout will fill the fragment | |
| mSwipeRefreshLayout.setLayoutParams( | |
| new ViewGroup.LayoutParams( | |
| ViewGroup.LayoutParams.MATCH_PARENT, | |
| ViewGroup.LayoutParams.MATCH_PARENT)); | |
| // Now return the SwipeRefreshLayout as this fragment's content view | |
| return mSwipeRefreshLayout; | |
| } | |
| /** | |
| * Set the SwipeRefreshLayout.OnRefreshListener to listen for initiated refreshes. | |
| * | |
| * @see android.support.v4.widget.SwipeRefreshLayout#setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener) | |
| */ | |
| public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) { | |
| mSwipeRefreshLayout.setOnRefreshListener(listener); | |
| } | |
| /** | |
| * Returns whether the SwipeRefreshLayout} is currently refreshing or not. | |
| * | |
| * @see android.support.v4.widget.SwipeRefreshLayout#isRefreshing() | |
| */ | |
| public boolean isRefreshing() { | |
| return mSwipeRefreshLayout.isRefreshing(); | |
| } | |
| /** | |
| * Set whether the SwipeRefreshLayout} should be displaying that it is refreshing or not. | |
| * | |
| * @see android.support.v4.widget.SwipeRefreshLayout#setRefreshing(boolean) | |
| */ | |
| public void setRefreshing(boolean refreshing) { | |
| mSwipeRefreshLayout.setRefreshing(refreshing); | |
| } | |
| /** | |
| * Set the color scheme for the SwipeRefreshLayout. | |
| * | |
| * @see android.support.v4.widget.SwipeRefreshLayout#setColorScheme(int, int, int, int) | |
| */ | |
| public void setColorScheme(int colorRes1, int colorRes2, int colorRes3, int colorRes4) { | |
| mSwipeRefreshLayout.setColorScheme(colorRes1, colorRes2, colorRes3, colorRes4); | |
| } | |
| /** | |
| * @return the fragment's SwipeRefreshLayout widget. | |
| */ | |
| public SwipeRefreshLayout getSwipeRefreshLayout() { | |
| return mSwipeRefreshLayout; | |
| } | |
| /** | |
| * Sub-class of SwipeRefreshLayout for use in this ListFragment. The reason that this is needed | |
| * is because SwipeRefreshLayout only supports a single child, which it expects to be the one | |
| * which triggers refreshes. In our case the layout's child is the content view returned from | |
| * ListFragment#onCreateView(LayoutInflater, ViewGroup, Bundle) which is a ViewGroup. | |
| * | |
| * To enable 'swipe-to-refresh' support via the ListView we need to override the default | |
| * behavior and properly signal when a gesture is possible. This is done by overriding | |
| * canChildScrollUp(). | |
| */ | |
| private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout { | |
| public ListFragmentSwipeRefreshLayout(Context context) { | |
| super(context); | |
| } | |
| /** | |
| * As mentioned above, we need to override this method to properly signal when a | |
| * 'swipe-to-refresh' is possible. | |
| * | |
| * @return true if the ListView is visible and can scroll up. | |
| */ | |
| @Override | |
| public boolean canChildScrollUp() { | |
| final ListView listView = getListView(); | |
| View emptyView = null; | |
| if (listView != null) { | |
| emptyView = listView.getEmptyView(); | |
| } | |
| if ( listView != null && listView.getVisibility() == VISIBLE ) { | |
| return ViewCompat.canScrollVertically(listView, -1); | |
| } else if(emptyView != null && emptyView.getVisibility() == VISIBLE) { | |
| return ViewCompat.canScrollVertically(emptyView, -1); | |
| } else { | |
| return false; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment