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
29/**
30 * Subclass of {@link androidx.fragment.app.ListFragment} which provides automatic support for
31 * providing the 'swipe-to-refresh' UX gesture by wrapping the the content view in a
32 * {@link androidx.swiperefreshlayout.widget.SwipeRefreshLayout}.
33 */
34public class SwipeRefreshListFragment extends ListFragment {
35
36 private boolean enabled = false;
37 private boolean refreshing = false;
38
39 private SwipeRefreshLayout.OnRefreshListener onRefreshListener;
40
41 private SwipeRefreshLayout mSwipeRefreshLayout;
42
43 @Override
44 public View onCreateView(LayoutInflater inflater, ViewGroup container,
45 Bundle savedInstanceState) {
46
47 // Create the list fragment's content view by calling the super method
48 final View listFragmentView = super.onCreateView(inflater, container, savedInstanceState);
49
50 // Now create a SwipeRefreshLayout to wrap the fragment's content view
51 mSwipeRefreshLayout = new ListFragmentSwipeRefreshLayout(container.getContext());
52 mSwipeRefreshLayout.setEnabled(enabled);
53 mSwipeRefreshLayout.setRefreshing(refreshing);
54
55 final Context context = getActivity();
56 if (context != null) {
57 // TODO are default colors fine here?
58 //mSwipeRefreshLayout.setColorSchemeColors(StyledAttributes.getColor(context, androidx.appcompat.R.attr.colorAccent));
59 }
60
61 if (onRefreshListener != null) {
62 mSwipeRefreshLayout.setOnRefreshListener(onRefreshListener);
63 }
64
65 // Add the list fragment's content view to the SwipeRefreshLayout, making sure that it fills
66 // the SwipeRefreshLayout
67 mSwipeRefreshLayout.addView(listFragmentView,
68 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
69
70 // Make sure that the SwipeRefreshLayout will fill the fragment
71 mSwipeRefreshLayout.setLayoutParams(
72 new ViewGroup.LayoutParams(
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 ViewGroup.LayoutParams.MATCH_PARENT));
75
76 // Now return the SwipeRefreshLayout as this fragment's content view
77 return mSwipeRefreshLayout;
78 }
79
80 /**
81 * Set the {@link androidx.core.widget.SwipeRefreshLayout.OnRefreshListener} to listen for
82 * initiated refreshes.
83 *
84 * @see androidx.core.widget.SwipeRefreshLayout#setOnRefreshListener(androidx.core.widget.SwipeRefreshLayout.OnRefreshListener)
85 */
86 public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
87 onRefreshListener = listener;
88 enabled = true;
89 if (mSwipeRefreshLayout != null) {
90 mSwipeRefreshLayout.setEnabled(true);
91 mSwipeRefreshLayout.setOnRefreshListener(listener);
92 }
93 }
94
95 /**
96 * Set whether the {@link androidx.core.widget.SwipeRefreshLayout} should be displaying
97 * that it is refreshing or not.
98 *
99 * @see androidx.core.widget.SwipeRefreshLayout#setRefreshing(boolean)
100 */
101 public void setRefreshing(boolean refreshing) {
102 this.refreshing = refreshing;
103 if (mSwipeRefreshLayout != null) {
104 mSwipeRefreshLayout.setRefreshing(refreshing);
105 }
106 }
107
108
109 /**
110 * Sub-class of {@link androidx.core.widget.SwipeRefreshLayout} for use in this
111 * {@link androidx.core.app.ListFragment}. The reason that this is needed is because
112 * {@link androidx.core.widget.SwipeRefreshLayout} only supports a single child, which it
113 * expects to be the one which triggers refreshes. In our case the layout's child is the content
114 * view returned from
115 * {@link androidx.core.app.ListFragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)}
116 * which is a {@link android.view.ViewGroup}.
117 *
118 * <p>To enable 'swipe-to-refresh' support via the {@link android.widget.ListView} we need to
119 * override the default behavior and properly signal when a gesture is possible. This is done by
120 * overriding {@link #canChildScrollUp()}.
121 */
122 private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout {
123
124 public ListFragmentSwipeRefreshLayout(Context context) {
125 super(context);
126 }
127
128 /**
129 * As mentioned above, we need to override this method to properly signal when a
130 * 'swipe-to-refresh' is possible.
131 *
132 * @return true if the {@link android.widget.ListView} is visible and can scroll up.
133 */
134 @Override
135 public boolean canChildScrollUp() {
136 final ListView listView = getListView();
137 if (listView.getVisibility() == View.VISIBLE) {
138 return listView.canScrollVertically(-1);
139 } else {
140 return false;
141 }
142 }
143
144 }
145
146}