SwipeRefreshListFragment.java

  1/*
  2 * Copyright 2014 The Android Open Source Project
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *       http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package eu.siacs.conversations.ui.widget;
 18
 19import android.content.Context;
 20import android.os.Bundle;
 21import android.view.LayoutInflater;
 22import android.view.View;
 23import android.view.ViewGroup;
 24import android.widget.ListView;
 25
 26import androidx.fragment.app.ListFragment;
 27import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 28
 29import eu.siacs.conversations.ui.util.StyledAttributes;
 30
 31/**
 32 * Subclass of {@link androidx.fragment.app.ListFragment} which provides automatic support for
 33 * providing the 'swipe-to-refresh' UX gesture by wrapping the the content view in a
 34 * {@link androidx.swiperefreshlayout.widget.SwipeRefreshLayout}.
 35 */
 36public class SwipeRefreshListFragment extends ListFragment {
 37
 38    private boolean enabled = false;
 39    private boolean refreshing = false;
 40
 41    private SwipeRefreshLayout.OnRefreshListener onRefreshListener;
 42
 43    private SwipeRefreshLayout mSwipeRefreshLayout;
 44
 45    @Override
 46    public View onCreateView(LayoutInflater inflater, ViewGroup container,
 47                             Bundle savedInstanceState) {
 48
 49        // Create the list fragment's content view by calling the super method
 50        final View listFragmentView = super.onCreateView(inflater, container, savedInstanceState);
 51
 52        // Now create a SwipeRefreshLayout to wrap the fragment's content view
 53        mSwipeRefreshLayout = new ListFragmentSwipeRefreshLayout(container.getContext());
 54        mSwipeRefreshLayout.setEnabled(enabled);
 55        mSwipeRefreshLayout.setRefreshing(refreshing);
 56
 57        final Context context = getActivity();
 58        if (context != null) {
 59            mSwipeRefreshLayout.setColorSchemeColors(StyledAttributes.getColor(context, androidx.appcompat.R.attr.colorAccent));
 60        }
 61
 62        if (onRefreshListener != null) {
 63            mSwipeRefreshLayout.setOnRefreshListener(onRefreshListener);
 64        }
 65
 66        // Add the list fragment's content view to the SwipeRefreshLayout, making sure that it fills
 67        // the SwipeRefreshLayout
 68        mSwipeRefreshLayout.addView(listFragmentView,
 69                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
 70
 71        // Make sure that the SwipeRefreshLayout will fill the fragment
 72        mSwipeRefreshLayout.setLayoutParams(
 73                new ViewGroup.LayoutParams(
 74                        ViewGroup.LayoutParams.MATCH_PARENT,
 75                        ViewGroup.LayoutParams.MATCH_PARENT));
 76
 77        // Now return the SwipeRefreshLayout as this fragment's content view
 78        return mSwipeRefreshLayout;
 79    }
 80
 81    /**
 82     * Set the {@link androidx.core.widget.SwipeRefreshLayout.OnRefreshListener} to listen for
 83     * initiated refreshes.
 84     *
 85     * @see androidx.core.widget.SwipeRefreshLayout#setOnRefreshListener(androidx.core.widget.SwipeRefreshLayout.OnRefreshListener)
 86     */
 87    public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
 88        onRefreshListener = listener;
 89        enabled = true;
 90        if (mSwipeRefreshLayout != null) {
 91            mSwipeRefreshLayout.setEnabled(true);
 92            mSwipeRefreshLayout.setOnRefreshListener(listener);
 93        }
 94    }
 95
 96    /**
 97     * Set whether the {@link androidx.core.widget.SwipeRefreshLayout} should be displaying
 98     * that it is refreshing or not.
 99     *
100     * @see androidx.core.widget.SwipeRefreshLayout#setRefreshing(boolean)
101     */
102    public void setRefreshing(boolean refreshing) {
103        this.refreshing = refreshing;
104        if (mSwipeRefreshLayout != null) {
105            mSwipeRefreshLayout.setRefreshing(refreshing);
106        }
107    }
108
109
110    /**
111     * Sub-class of {@link androidx.core.widget.SwipeRefreshLayout} for use in this
112     * {@link androidx.core.app.ListFragment}. The reason that this is needed is because
113     * {@link androidx.core.widget.SwipeRefreshLayout} only supports a single child, which it
114     * expects to be the one which triggers refreshes. In our case the layout's child is the content
115     * view returned from
116     * {@link androidx.core.app.ListFragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)}
117     * which is a {@link android.view.ViewGroup}.
118     *
119     * <p>To enable 'swipe-to-refresh' support via the {@link android.widget.ListView} we need to
120     * override the default behavior and properly signal when a gesture is possible. This is done by
121     * overriding {@link #canChildScrollUp()}.
122     */
123    private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout {
124
125        public ListFragmentSwipeRefreshLayout(Context context) {
126            super(context);
127        }
128
129        /**
130         * As mentioned above, we need to override this method to properly signal when a
131         * 'swipe-to-refresh' is possible.
132         *
133         * @return true if the {@link android.widget.ListView} is visible and can scroll up.
134         */
135        @Override
136        public boolean canChildScrollUp() {
137            final ListView listView = getListView();
138            if (listView.getVisibility() == View.VISIBLE) {
139                return listView.canScrollVertically(-1);
140            } else {
141                return false;
142            }
143        }
144
145    }
146
147}