securesmartcam / app / src / main / java / org / witness / obscuracam / ui / AlbumLayoutManager.java @ 41590feb
History | View | Annotate | Download (2.14 KB)
1 |
package org.witness.obscuracam.ui; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
import android.support.v7.widget.GridLayoutManager; |
5 |
import android.support.v7.widget.RecyclerView; |
6 |
import android.util.TypedValue; |
7 |
|
8 |
public class AlbumLayoutManager extends GridLayoutManager { |
9 |
|
10 |
private int mColumnWidth; |
11 |
private boolean mColumnWidthChanged = true; |
12 |
|
13 |
public AlbumLayoutManager(Context context, int columnWidth) { |
14 |
super(context, 1); |
15 |
setColumnWidth(checkedColumnWidth(context, columnWidth)); |
16 |
} |
17 |
|
18 |
public AlbumLayoutManager(Context context, int columnWidth, int orientation, boolean reverseLayout) { |
19 |
/* Initially set spanCount to 1, will be changed automatically later. */
|
20 |
super(context, 1, orientation, reverseLayout); |
21 |
setColumnWidth(checkedColumnWidth(context, columnWidth)); |
22 |
} |
23 |
|
24 |
private int checkedColumnWidth(Context context, int columnWidth) { |
25 |
if (columnWidth <= 0) { |
26 |
/* Set default columnWidth value (48dp here). It is better to move this constant
|
27 |
to static constant on top, but we need context to convert it to dp, so can't really
|
28 |
do so. */
|
29 |
columnWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, |
30 |
context.getResources().getDisplayMetrics()); |
31 |
} |
32 |
return columnWidth;
|
33 |
} |
34 |
|
35 |
public void setColumnWidth(int newColumnWidth) { |
36 |
if (newColumnWidth > 0 && newColumnWidth != mColumnWidth) { |
37 |
mColumnWidth = newColumnWidth; |
38 |
mColumnWidthChanged = true;
|
39 |
} |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { |
44 |
if (mColumnWidthChanged && mColumnWidth > 0) { |
45 |
int totalSpace;
|
46 |
if (getOrientation() == VERTICAL) {
|
47 |
totalSpace = getWidth() - getPaddingRight() - getPaddingLeft(); |
48 |
} else {
|
49 |
totalSpace = getHeight() - getPaddingTop() - getPaddingBottom(); |
50 |
} |
51 |
int spanCount = Math.max(1, totalSpace / mColumnWidth); |
52 |
setSpanCount(spanCount); |
53 |
mColumnWidthChanged = false;
|
54 |
} |
55 |
super.onLayoutChildren(recycler, state);
|
56 |
} |
57 |
} |