15

I'm having an issue with the SwipeRefreshLayout. I have a list view within the layout and every time I scroll upward in the list view the swipe to refresh layout is tiggered and you can never scroll back to the top of the layout. Anyone have any ideas?

VirtualProdigy
  • 1,637
  • 1
  • 20
  • 40

2 Answers2

29

I found an answer to my question. I am manually enabling the swipeRefreshLayout when the first child in the listview is at the top of the list.

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView listView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ?
                            0 : expandableListview.getChildAt(0).getTop();
            refresh.setEnabled((topRowVerticalPosition >= 0));
        }
    });
VirtualProdigy
  • 1,637
  • 1
  • 20
  • 40
  • Thanks, I have the same Problem with Api 10 ... its not in newer Apis. Seems to be a bug – Mate Mar 13 '15 at 11:54
  • 1
    Same solution, just a bit easier to read here at end of post: https://yassirh.com/2014/05/how-to-use-swiperefreshlayout-the-right-way/ – Alexandre G Apr 15 '15 at 09:17
  • 3
    This works fine if the ListView is the only child of the SwipeLayout but if there are other views in addition to the ListView and you want to enable the SwipeLayout if there is a touch and pull in any other location besides the ListView, then this solution will not work. – AlanKley Jun 12 '15 at 22:04
  • @AliAnsari [ExpandableListView](http://developer.android.com/reference/android/widget/ExpandableListView.html) – VirtualProdigy Jul 28 '15 at 16:23
  • @AlexandreG This solution is working fine here and very clear! Thanks for sharing. – Mohammed AlBanna Dec 09 '15 at 02:19
  • after applying the following solution, I am facing problem if the 1st row in the list view has height greater than the screen size. It wont load other rows on screen and not able to scroll. If the height of 1st row is less than the screen size, than all works as expected. – Malav Shah Oct 25 '16 at 21:59
4

https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

According to Android docs at the above link,

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.

If the ListView is the 1st direct child of the SwipeRefreshLayout, refresh won't be triggered before scrolling back to the top of the ListView. You don't need to do anything in onScroll().

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Alpha Huang
  • 1,297
  • 1
  • 12
  • 15