Java tutorial
/* * Copyright 2016 TomeOkin * * 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.tomeokin.lspush.ui.widget; import android.content.Context; import android.support.v4.view.ViewCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * http://innodroid.com/blog/post/recycler-empty-swipe-refresh */ public class SwipeRefreshLayoutWithEmpty extends SwipeRefreshLayout { private ViewGroup container; public SwipeRefreshLayoutWithEmpty(Context context) { super(context); } public SwipeRefreshLayoutWithEmpty(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean canChildScrollUp() { // The swipe refresh layout has 2 children; the circle refresh indicator // and the view container. The container is needed here ViewGroup container = getContainer(); if (container == null) { return false; } // The container has 2 children; the empty view and the scrollable view. if (container.getChildCount() != 2) { throw new RuntimeException("Container must have an empty view and content view"); } // Use whichever one is visible and test that it can scroll View view = container.getChildAt(0); if (view.getVisibility() != View.VISIBLE) { view = container.getChildAt(1); } return ViewCompat.canScrollVertically(view, -1); } private ViewGroup getContainer() { // Cache this view if (container != null) { return container; } // The container may not be the first view. Need to iterate to find it final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { if (getChildAt(i) instanceof ViewGroup) { container = (ViewGroup) getChildAt(i); break; } } return container; } }