4
4
import android .app .Fragment ;
5
5
import android .content .Intent ;
6
6
import android .content .SharedPreferences ;
7
- import android .content .res .Configuration ;
8
7
import android .graphics .drawable .NinePatchDrawable ;
9
8
import android .os .Build ;
10
9
import android .os .Bundle ;
14
13
import android .support .v4 .widget .SwipeRefreshLayout ;
15
14
import android .support .v7 .widget .DefaultItemAnimator ;
16
15
import android .support .v7 .widget .GridLayoutManager ;
17
- import android .support .v7 .widget .LinearLayoutManager ;
18
16
import android .support .v7 .widget .RecyclerView ;
19
17
import android .view .LayoutInflater ;
20
18
import android .view .Menu ;
21
19
import android .view .MenuInflater ;
22
20
import android .view .MenuItem ;
23
21
import android .view .View ;
24
22
import android .view .ViewGroup ;
25
- import android .view .ViewTreeObserver ;
26
23
import android .widget .Button ;
27
24
import android .widget .PopupMenu ;
28
25
import android .widget .TextView ;
38
35
import oly .netpowerctrl .executables .adapter .AdapterSource ;
39
36
import oly .netpowerctrl .executables .adapter .ExecutableAdapterItem ;
40
37
import oly .netpowerctrl .executables .adapter .ExecutableViewHolder ;
38
+ import oly .netpowerctrl .executables .adapter .ExecutablesAdapter ;
41
39
import oly .netpowerctrl .executables .adapter .ExecutablesEditableAdapter ;
42
40
import oly .netpowerctrl .executables .adapter .FilterByHidden ;
43
41
import oly .netpowerctrl .executables .adapter .FilterByReachable ;
@@ -77,31 +75,15 @@ public class ExecutablesFragment extends Fragment implements PopupMenu.OnMenuIte
77
75
private RecyclerView mRecyclerView ;
78
76
private SwipeRefreshLayout mPullToRefreshLayout ;
79
77
private View btnWireless ;
80
- private int requestedColumnWidth ;
81
78
// Adapter
82
79
private AdapterSource adapterSource ;
83
80
private ExecutablesEditableAdapter adapter ;
84
- private final ViewTreeObserver .OnGlobalLayoutListener mListViewNumColumnsChangeListener =
85
- new ViewTreeObserver .OnGlobalLayoutListener () {
86
- @ Override
87
- public void onGlobalLayout () {
88
- //noinspection deprecation
89
- mRecyclerView .getViewTreeObserver ().removeOnGlobalLayoutListener (mListViewNumColumnsChangeListener );
90
-
91
- int i = mRecyclerView .getWidth () / requestedColumnWidth ;
92
- if (i < 1 ) i = 1 ;
93
- adapter .setItemsInRow (i );
94
- GridLayoutManager gridLayoutManager = new GridLayoutManager (getActivity (), i );
95
- gridLayoutManager .setSpanSizeLookup (getAdapter ().getSpanSizeLookup ());
96
- mRecyclerView .setHasFixedSize (false );
97
- mRecyclerView .setLayoutManager (gridLayoutManager );
98
- mRecyclerView .setAdapter (adapter );
99
- }
100
- };
101
81
private String clicked_group_uid ;
102
82
// UI
103
83
private boolean editMode ;
104
84
private SimpleListDividerDecoration listDividerDecoration ;
85
+ private GridLayoutManager gridLayoutManager ;
86
+ private int requestedColumnWidth = 0 ;
105
87
private View btnAdd ;
106
88
private View btnEdit ;
107
89
// Data
@@ -112,15 +94,6 @@ public void onGlobalLayout() {
112
94
public ExecutablesFragment () {
113
95
}
114
96
115
- @ Override
116
- public void onConfigurationChanged (Configuration newConfig ) {
117
- super .onConfigurationChanged (newConfig );
118
-
119
- if (mRecyclerView != null ) {
120
- mRecyclerView .getViewTreeObserver ().addOnGlobalLayoutListener (mListViewNumColumnsChangeListener );
121
- }
122
- }
123
-
124
97
@ Override
125
98
public void onRefresh () {
126
99
dataService .refreshExistingDevices ();
@@ -169,26 +142,30 @@ private void applyViewType() {
169
142
int viewType = SharedPrefs .getInstance ().getOutletsViewType ();
170
143
171
144
mRecyclerView .removeItemDecoration (listDividerDecoration );
145
+ int columns ;
172
146
173
147
switch (viewType ) {
174
148
case VIEW_AS_COMPACT :
175
- adapter .setLayoutRes (R .layout .grid_item_compact_executable );
176
149
requestedColumnWidth = (int ) getResources ().getDimension (R .dimen .min_grid_item_width );
150
+ columns = Math .max (mRecyclerView .getWidth () / requestedColumnWidth , 1 );
151
+ gridLayoutManager .setSpanCount (columns );
152
+ adapter .setLayoutRes (R .layout .grid_item_compact_executable );
177
153
break ;
178
154
case VIEW_AS_GRID :
179
- adapter .setLayoutRes (R .layout .grid_item_executable );
180
155
requestedColumnWidth = (int ) getResources ().getDimension (R .dimen .min_grid_item_width );
156
+ columns = Math .max (mRecyclerView .getWidth () / requestedColumnWidth , 1 );
157
+ gridLayoutManager .setSpanCount (columns );
158
+ adapter .setLayoutRes (R .layout .grid_item_executable );
181
159
break ;
182
160
case VIEW_AS_LIST :
183
161
default :
184
162
mRecyclerView .addItemDecoration (listDividerDecoration );
185
- adapter .setLayoutRes (R .layout .list_item_executable );
186
163
requestedColumnWidth = (int ) getResources ().getDimension (R .dimen .min_list_item_width );
164
+ columns = Math .max (mRecyclerView .getWidth () / requestedColumnWidth , 1 );
165
+ gridLayoutManager .setSpanCount (columns );
166
+ adapter .setLayoutRes (R .layout .list_item_executable );
187
167
break ;
188
168
}
189
-
190
- mRecyclerView .getViewTreeObserver ().addOnGlobalLayoutListener (mListViewNumColumnsChangeListener );
191
- mRecyclerView .requestLayout ();
192
169
}
193
170
194
171
@ Override
@@ -217,12 +194,14 @@ public void onClick(View view) {
217
194
218
195
mRecyclerView = (RecyclerView ) view .findViewById (android .R .id .list );
219
196
mRecyclerView .setItemAnimator (new DefaultItemAnimator ());
220
- mRecyclerView .setLayoutManager (new LinearLayoutManager (getActivity ()));
221
197
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
222
198
mRecyclerView .addItemDecoration (new ItemShadowDecoration ((NinePatchDrawable ) ContextCompat .getDrawable (getActivity (), R .drawable .material_shadow_z1 )));
223
199
}
224
200
listDividerDecoration = new SimpleListDividerDecoration (ContextCompat .getDrawable (getActivity (), R .drawable .list_divider ), true );
225
201
mRecyclerView .addOnItemTouchListener (new RecyclerItemClickListener (getActivity (), this , null ));
202
+ gridLayoutManager = new GridLayoutManager (getActivity (), 1 );
203
+ mRecyclerView .setHasFixedSize (false );
204
+ mRecyclerView .setLayoutManager (gridLayoutManager );
226
205
227
206
automaticSetup = new AutomaticSetup (
228
207
(Button ) view .findViewById (R .id .automatic_setup_start ),
@@ -249,7 +228,13 @@ public void onClick(View view) {
249
228
adapterSource .addInput (new InputExecutables (), new InputGroupChanges ());
250
229
251
230
adapter = new ExecutablesEditableAdapter (adapterSource , LoadStoreIconData .iconLoadingThread );
252
-
231
+ adapter .setItemsInRow (new ExecutablesAdapter .ItemsInRow () {
232
+ @ Override
233
+ public int getItemsInRow () {
234
+ return gridLayoutManager .getSpanCount ();
235
+ }
236
+ });
237
+ gridLayoutManager .setSpanSizeLookup (adapter .getSpanSizeLookup ());
253
238
adapter .getSource ().setEmptyListener (this );
254
239
255
240
///// For pull to refresh
@@ -296,6 +281,18 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
296
281
}
297
282
});
298
283
284
+ mRecyclerView .addOnLayoutChangeListener (new View .OnLayoutChangeListener () {
285
+ int lastWidth = 0 ;
286
+
287
+ @ Override
288
+ public void onLayoutChange (View view , int i , int i1 , int i2 , int i3 , int i4 , int i5 , int i6 , int i7 ) {
289
+ if (requestedColumnWidth == 0 || mRecyclerView .getWidth () == lastWidth ) return ;
290
+ lastWidth = mRecyclerView .getWidth ();
291
+ int columns = Math .max (lastWidth / requestedColumnWidth , 1 );
292
+ gridLayoutManager .setSpanCount (columns );
293
+ }
294
+ });
295
+
299
296
LoadStoreIconData .iconCacheClearedObserver .register (this );
300
297
DataService .observersStartStopRefresh .register (this );
301
298
DataService .observersServiceReady .register (this ); // Will call onServiceReady and setup current group
0 commit comments