Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Swipe Refresh Layout scroll up to refresh

I've implemented a swipe refresh layout for my grid view and it works perfectly if I pull (scroll) downwards for the refresh to be called.

However, I'm trying to figure out how to refresh when the user scrolls up. So when the user reaches the last of the items in the grid (my limit is 10) so when the user sees all ten and then pulls upwards or tries to continue to scroll how can I then refresh?

Any help is appreciated.

like image 377
DJ-DOO Avatar asked Jul 17 '14 11:07

DJ-DOO


People also ask

How do you swipe to refresh on 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 do I stop swipe refresh layout?

To disable the gesture and progress animation, call setEnabled(false) on the view. This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.

What is SwipeRefreshLayout in Android?

Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView. The basic need for a SwipeRefreshLayout is to allow the users to refresh the screen manually. This is pretty common in the Facebook Newsfeed screen.


2 Answers

See my answer in stackoverflow

SwipeRefreshLayout from Android Support Library version 21 does not support pull from bottom. I have made modification SwipeRefreshLayoutBottom with is based on original SwipeRefreshLayout code. It is fully based on original Google code with just inversion of coordinates and newly writed canChildScrollDown method. All modification are marked as TODO.

Bitbucker repository

like image 132
AndreyICE Avatar answered Oct 06 '22 15:10

AndreyICE


so people either a: don't know the answer to this question or b: they're not willing to share the knowledge.

Anyway after a long time I came up with my own solution. I added a scroll listener to my grid, and calculated when I scrolled to the bottom of the screen, at this point I recall the onRefresh() method from the swipe to refresh...

EDIT to add further info

So, after I did some research to calculate when the user reached the bottom of a grid view I followed questions like this and others to get that. Below is my code that I had to alter in order have it work with the native refreshing functionality. My alterations were to prevent constant refreshing. So included in my if statement is a check that it's not currently refreshing, so when I reached the bottom it refreshed, but I didn't have that check so it constantly refreshed. I then had to do a check against the total amount of items in my grid, as when the user reached the last item (very last item) the app also kept refreshing and there was nothing to break the cycle. Hopefully this is of some help...

        worldBeersGrid.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }
        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            int position = firstVisibleItem+visibleItemCount;
            int limit = totalItemCount;
            int totalItems = sharedpreferences.getInt("WORLD ITEM COUNT", 0);

            Log.i("TOTAL WORLD ITEMS", String.valueOf(totalItems));
            Log.i("LIMIT :::::::::::: ", String.valueOf(limit));
            Log.i("POSITION ::::::::::::", String.valueOf(position));
            Log.i("REFRESHING :::::::::::::::", String.valueOf(swipeRefreshLayout.isRefreshing()));

            if(position>=limit && totalItemCount>0 && !swipeRefreshLayout.isRefreshing() && position <totalItems){
                swipeRefreshLayout.setRefreshing(true);
                //In the below method I made my call to my ws...
                onRefresh();
            }
        }
    });
like image 26
DJ-DOO Avatar answered Oct 06 '22 14:10

DJ-DOO