Skip to content

Commit 77fff9e

Browse files
author
David Graeff
committed
Theme improvements. Rotation and span computation fixed for outlet view.
1 parent f29b5af commit 77fff9e

File tree

8 files changed

+90
-80
lines changed

8 files changed

+90
-80
lines changed

app/src/main/java/oly/netpowerctrl/executables/ExecutablesFragment.java

+35-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.app.Fragment;
55
import android.content.Intent;
66
import android.content.SharedPreferences;
7-
import android.content.res.Configuration;
87
import android.graphics.drawable.NinePatchDrawable;
98
import android.os.Build;
109
import android.os.Bundle;
@@ -14,15 +13,13 @@
1413
import android.support.v4.widget.SwipeRefreshLayout;
1514
import android.support.v7.widget.DefaultItemAnimator;
1615
import android.support.v7.widget.GridLayoutManager;
17-
import android.support.v7.widget.LinearLayoutManager;
1816
import android.support.v7.widget.RecyclerView;
1917
import android.view.LayoutInflater;
2018
import android.view.Menu;
2119
import android.view.MenuInflater;
2220
import android.view.MenuItem;
2321
import android.view.View;
2422
import android.view.ViewGroup;
25-
import android.view.ViewTreeObserver;
2623
import android.widget.Button;
2724
import android.widget.PopupMenu;
2825
import android.widget.TextView;
@@ -38,6 +35,7 @@
3835
import oly.netpowerctrl.executables.adapter.AdapterSource;
3936
import oly.netpowerctrl.executables.adapter.ExecutableAdapterItem;
4037
import oly.netpowerctrl.executables.adapter.ExecutableViewHolder;
38+
import oly.netpowerctrl.executables.adapter.ExecutablesAdapter;
4139
import oly.netpowerctrl.executables.adapter.ExecutablesEditableAdapter;
4240
import oly.netpowerctrl.executables.adapter.FilterByHidden;
4341
import oly.netpowerctrl.executables.adapter.FilterByReachable;
@@ -77,31 +75,15 @@ public class ExecutablesFragment extends Fragment implements PopupMenu.OnMenuIte
7775
private RecyclerView mRecyclerView;
7876
private SwipeRefreshLayout mPullToRefreshLayout;
7977
private View btnWireless;
80-
private int requestedColumnWidth;
8178
// Adapter
8279
private AdapterSource adapterSource;
8380
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-
};
10181
private String clicked_group_uid;
10282
// UI
10383
private boolean editMode;
10484
private SimpleListDividerDecoration listDividerDecoration;
85+
private GridLayoutManager gridLayoutManager;
86+
private int requestedColumnWidth = 0;
10587
private View btnAdd;
10688
private View btnEdit;
10789
// Data
@@ -112,15 +94,6 @@ public void onGlobalLayout() {
11294
public ExecutablesFragment() {
11395
}
11496

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-
12497
@Override
12598
public void onRefresh() {
12699
dataService.refreshExistingDevices();
@@ -169,26 +142,30 @@ private void applyViewType() {
169142
int viewType = SharedPrefs.getInstance().getOutletsViewType();
170143

171144
mRecyclerView.removeItemDecoration(listDividerDecoration);
145+
int columns;
172146

173147
switch (viewType) {
174148
case VIEW_AS_COMPACT:
175-
adapter.setLayoutRes(R.layout.grid_item_compact_executable);
176149
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);
177153
break;
178154
case VIEW_AS_GRID:
179-
adapter.setLayoutRes(R.layout.grid_item_executable);
180155
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);
181159
break;
182160
case VIEW_AS_LIST:
183161
default:
184162
mRecyclerView.addItemDecoration(listDividerDecoration);
185-
adapter.setLayoutRes(R.layout.list_item_executable);
186163
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);
187167
break;
188168
}
189-
190-
mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(mListViewNumColumnsChangeListener);
191-
mRecyclerView.requestLayout();
192169
}
193170

194171
@Override
@@ -217,12 +194,14 @@ public void onClick(View view) {
217194

218195
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
219196
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
220-
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
221197
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
222198
mRecyclerView.addItemDecoration(new ItemShadowDecoration((NinePatchDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.material_shadow_z1)));
223199
}
224200
listDividerDecoration = new SimpleListDividerDecoration(ContextCompat.getDrawable(getActivity(), R.drawable.list_divider), true);
225201
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), this, null));
202+
gridLayoutManager = new GridLayoutManager(getActivity(), 1);
203+
mRecyclerView.setHasFixedSize(false);
204+
mRecyclerView.setLayoutManager(gridLayoutManager);
226205

227206
automaticSetup = new AutomaticSetup(
228207
(Button) view.findViewById(R.id.automatic_setup_start),
@@ -249,7 +228,13 @@ public void onClick(View view) {
249228
adapterSource.addInput(new InputExecutables(), new InputGroupChanges());
250229

251230
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());
253238
adapter.getSource().setEmptyListener(this);
254239

255240
///// For pull to refresh
@@ -296,6 +281,18 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
296281
}
297282
});
298283

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+
299296
LoadStoreIconData.iconCacheClearedObserver.register(this);
300297
DataService.observersStartStopRefresh.register(this);
301298
DataService.observersServiceReady.register(this); // Will call onServiceReady and setup current group

app/src/main/java/oly/netpowerctrl/executables/ExecutablesViewModeDialog.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
4949
ExecutablesAdapter adapter;
5050
AdapterSource adapterSource;
5151
GridLayoutManager gridLayoutManager;
52-
int rows;
5352

54-
rows = 1;
5553
adapterSource = new AdapterSource(AdapterSource.AutoStartEnum.AutoStartOnServiceReady);
5654
adapterSource.addInput(new InputDemo());
5755
adapter = new ExecutablesAdapter(adapterSource, LoadStoreIconData.iconLoadingThread, R.layout.list_item_executable);
58-
adapter.setItemsInRow(rows);
56+
adapter.setItemsInRow(new ExecutablesAdapter.ItemsInRow() {
57+
@Override
58+
public int getItemsInRow() {
59+
return 1;
60+
}
61+
});
5962
recyclerView = (RecyclerView) rootView.findViewById(R.id.list1);
6063
recyclerView.setItemAnimator(new DefaultItemAnimator());
6164
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
6265
recyclerView.setAdapter(adapter);
63-
gridLayoutManager = new GridLayoutManager(getActivity(), rows);
66+
gridLayoutManager = new GridLayoutManager(getActivity(), 1);
6467
gridLayoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
6568
recyclerView.setHasFixedSize(false);
6669
recyclerView.setLayoutManager(gridLayoutManager);
@@ -79,16 +82,20 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
7982
}
8083
});
8184

82-
rows = 2;
8385
adapterSource = new AdapterSource(AdapterSource.AutoStartEnum.AutoStartOnServiceReady);
8486
adapterSource.addInput(new InputDemo());
8587
adapter = new ExecutablesAdapter(adapterSource, LoadStoreIconData.iconLoadingThread, R.layout.grid_item_executable);
86-
adapter.setItemsInRow(rows);
88+
adapter.setItemsInRow(new ExecutablesAdapter.ItemsInRow() {
89+
@Override
90+
public int getItemsInRow() {
91+
return 2;
92+
}
93+
});
8794
recyclerView = (RecyclerView) rootView.findViewById(R.id.list2);
8895
recyclerView.setItemAnimator(new DefaultItemAnimator());
8996
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
9097
recyclerView.setAdapter(adapter);
91-
gridLayoutManager = new GridLayoutManager(getActivity(), rows);
98+
gridLayoutManager = new GridLayoutManager(getActivity(), 2);
9299
gridLayoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
93100
recyclerView.setHasFixedSize(false);
94101
recyclerView.setLayoutManager(gridLayoutManager);
@@ -110,12 +117,17 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
110117
adapterSource = new AdapterSource(AdapterSource.AutoStartEnum.AutoStartOnServiceReady);
111118
adapterSource.addInput(new InputDemo());
112119
adapter = new ExecutablesAdapter(adapterSource, LoadStoreIconData.iconLoadingThread, R.layout.grid_item_compact_executable);
113-
adapter.setItemsInRow(rows);
120+
adapter.setItemsInRow(new ExecutablesAdapter.ItemsInRow() {
121+
@Override
122+
public int getItemsInRow() {
123+
return 2;
124+
}
125+
});
114126
recyclerView = (RecyclerView) rootView.findViewById(R.id.list3);
115127
recyclerView.setItemAnimator(new DefaultItemAnimator());
116128
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
117129
recyclerView.setAdapter(adapter);
118-
gridLayoutManager = new GridLayoutManager(getActivity(), rows);
130+
gridLayoutManager = new GridLayoutManager(getActivity(), 2);
119131
gridLayoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
120132
recyclerView.setHasFixedSize(false);
121133
recyclerView.setLayoutManager(gridLayoutManager);

app/src/main/java/oly/netpowerctrl/executables/adapter/ExecutablesAdapter.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ public class ExecutablesAdapter extends RecyclerView.Adapter<ExecutableViewHolde
1414
protected final AdapterSource mSource;
1515
private final IconDeferredLoadingThread mIconLoadThread;
1616
protected int mItemsResourceId = 0;
17-
18-
private int mItemsInRow = 1;
17+
private ItemsInRow itemsInRow = new ItemsInRow() {
18+
@Override
19+
public int getItemsInRow() {
20+
return 1;
21+
}
22+
};
1923
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
2024
@Override
2125
public int getSpanSize(int position) {
2226
final ExecutableAdapterItem item = mSource.mItems.get(position);
23-
final Executable executable = item.getExecutable();
24-
if (executable == null)
25-
return mItemsInRow;
27+
if (item.getExecutable() == null)
28+
return itemsInRow.getItemsInRow(); // Only one header per row -> span full row
2629
else
27-
return 1; // Only one header per row
30+
return 1;
2831
}
2932
};
3033

@@ -44,6 +47,9 @@ public AdapterSource getSource() {
4447

4548
public void setLayoutRes(int layout_res) {
4649
this.mItemsResourceId = layout_res;
50+
// viewholder have to be recreated
51+
notifyItemRangeRemoved(0, mSource.mItems.size());
52+
notifyItemRangeInserted(0, mSource.mItems.size());
4753
}
4854

4955
@Override
@@ -103,14 +109,13 @@ public int getItemCount() {
103109
return mSource.mItems.size();
104110
}
105111

106-
//////////////// Group Spans //////////////
107-
108-
public void setItemsInRow(int itemsInRow) {
109-
if (itemsInRow == this.mItemsInRow)
110-
return;
112+
public void setItemsInRow(ItemsInRow itemsInRow) {
113+
this.itemsInRow = itemsInRow;
114+
}
111115

112-
this.mItemsInRow = itemsInRow;
116+
//////////////// Group Spans //////////////
113117

114-
notifyDataSetChanged();
118+
public interface ItemsInRow {
119+
int getItemsInRow();
115120
}
116121
}

app/src/main/res/layout/activity_edit.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<ScrollView
88
android:id="@+id/scroll_vertical"
99
android:layout_width="match_parent"
10-
android:layout_height="match_parent"
11-
android:background="?attr/activity_background">
10+
android:layout_height="match_parent">
1211

1312
<LinearLayout
1413
android:layout_width="match_parent"

app/src/main/res/layout/activity_main.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
android:layout_width="match_parent"
4141
android:layout_height="fill_parent"
4242
android:layout_above="@id/toolbar_bottom_actionbar"
43-
android:layout_below="@id/toolbar_container"
44-
android:background="?attr/activity_background" />
43+
android:layout_below="@id/toolbar_container" />
4544

4645
<com.rey.material.widget.FloatingActionButton
4746
android:id="@+id/btnEdit"

app/src/main/res/layout/list_item_executable.xml

+12-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
android:layout_height="wrap_content"
77
android:layout_marginEnd="10dp"
88
android:layout_marginStart="10dp"
9-
android:background="?attr/activity_background"
9+
android:background="?attr/cards_background"
10+
android:elevation="3dp"
1011
android:gravity="center_vertical"
1112
android:minWidth="280dp"
12-
android:paddingEnd="10dp">
13+
android:paddingEnd="10dp"
14+
android:paddingStart="0dp">
1315

1416
<ImageView
1517
android:id="@+id/icon_bitmap"
1618
android:layout_width="58dp"
1719
android:layout_height="52dp"
18-
android:layout_marginRight="5dp"
20+
android:layout_marginEnd="5dp"
1921
android:background="?attr/colorPrimaryTransparent"
2022
android:paddingBottom="2dp"
2123
android:paddingLeft="5dp"
@@ -27,9 +29,9 @@
2729
android:id="@+id/progress"
2830
android:layout_width="58dp"
2931
android:layout_height="52dp"
30-
android:layout_alignLeft="@id/icon_bitmap"
32+
android:layout_alignStart="@id/icon_bitmap"
3133
android:layout_alignTop="@id/icon_bitmap"
32-
android:layout_marginRight="5dp"
34+
android:layout_marginEnd="5dp"
3335
android:indeterminate="true"
3436
android:paddingBottom="2dp"
3537
android:paddingLeft="5dp"
@@ -42,9 +44,10 @@
4244
android:layout_width="wrap_content"
4345
android:layout_height="wrap_content"
4446
android:layout_centerVertical="true"
45-
android:layout_toRightOf="@id/icon_bitmap"
47+
android:layout_toEndOf="@id/icon_bitmap"
4648
android:ellipsize="marquee"
47-
android:paddingRight="5dp"
49+
android:paddingEnd="5dp"
50+
android:paddingStart="0dp"
4851
android:singleLine="true"
4952
android:textAppearance="@android:style/TextAppearance.Medium" />
5053

@@ -53,7 +56,7 @@
5356
android:layout_width="wrap_content"
5457
android:layout_height="wrap_content"
5558
android:layout_centerVertical="true"
56-
android:layout_toRightOf="@id/title"
59+
android:layout_toEndOf="@id/title"
5760
android:ellipsize="end"
5861
android:paddingTop="2dp"
5962
android:singleLine="true"
@@ -64,7 +67,7 @@
6467
android:layout_width="match_parent"
6568
android:layout_height="wrap_content"
6669
android:layout_below="@id/title"
67-
android:layout_toRightOf="@id/icon_bitmap"
70+
android:layout_toEndOf="@id/icon_bitmap"
6871
android:focusable="false"
6972
android:focusableInTouchMode="false"
7073
android:paddingBottom="5dp"

app/src/main/res/values/attrs.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<!-- background colors -->
4-
<attr name="activity_background" format="color" />
54
<attr name="cards_background" format="color" />
65
<attr name="transparent_rounded_background" format="reference" />
76

0 commit comments

Comments
 (0)