davidmigloz/go-bees

View on GitHub
app/src/main/java/com/davidmiguel/gobees/utils/ScrollChildSwipeRefreshLayout.java

Summary

Maintainability
A
0 mins
Test Coverage
/*
 * Copyright 2016, The Android Open Source Project
 *
 * 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.davidmiguel.gobees.utils;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;

/**
 * Extends SwipeRefreshLayout to support non-direct descendant scrolling views.
 * SwipeRefreshLayout works as expected when a scroll view is a direct child: it triggers
 * the refresh only when the view is on top. This class adds a way setScrollUpChild to
 * define which view controls this behavior.
 */
public class ScrollChildSwipeRefreshLayout extends SwipeRefreshLayout {

    private View mScrollUpChild;

    public ScrollChildSwipeRefreshLayout(Context context) {
        super(context);
    }

    public ScrollChildSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean canChildScrollUp() {
        if (mScrollUpChild != null) {
            return ViewCompat.canScrollVertically(mScrollUpChild, -1);
        }
        return super.canChildScrollUp();
    }

    /**
     * Child view to attach SwipeRefreshLayout.
     *
     * @param view child view.
     */
    public void setScrollUpChild(View view) {
        mScrollUpChild = view;
    }
}