Skip to content

Instantly share code, notes, and snippets.

@UweTrottmann
Created April 21, 2015 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UweTrottmann/150af4a94e5224ae77a0 to your computer and use it in GitHub Desktop.
Save UweTrottmann/150af4a94e5224ae77a0 to your computer and use it in GitHub Desktop.
Android SwipeRefreshLayout with multiple swipeable children. Min API 14.
/*
* Copyright 2014 Uwe Trottmann
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
/**
* A {@link android.support.v4.widget.SwipeRefreshLayout} which only checks the (scrollable!) views
* set through {@link #setSwipeableChildren(int...)} if they can scroll up to determine whether to
* trigger the refresh gesture.
*/
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
private View[] swipeableChildren;
public MultiSwipeRefreshLayout(Context context) {
super(context);
}
public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Set the children to be checked if they can scroll up and hence should prevent the refresh
* gesture from being triggered.
*/
public void setSwipeableChildren(final int... ids) {
if (ids == null) {
return;
}
// find child views
swipeableChildren = new View[ids.length];
for (int i = 0; i < ids.length; i++) {
View view = findViewById(ids[i]);
if (view == null) {
throw new IllegalArgumentException(
"Supplied view ids need to exist in this layout.");
}
swipeableChildren[i] = view;
}
}
@Override
public boolean canChildScrollUp() {
if (swipeableChildren != null) {
// check if any supplied swipeable children can scroll up
for (View view : swipeableChildren) {
if (view.isShown() && ViewCompat.canScrollVertically(view, -1)) {
// prevent refresh gesture
return true;
}
}
}
return false;
}
}
@nolanamy
Copy link

nolanamy commented Aug 7, 2018

Nice. Decent explanation of the issue: http://innodroid.com/blog/post/recycler-empty-swipe-refresh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment