1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package de.pixart.messenger.ui.util;
import android.content.Context;
import androidx.annotation.DimenRes;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.ViewTreeObserver;
import de.pixart.messenger.Config;
import de.pixart.messenger.ui.adapter.MediaAdapter;
public class GridManager {
public static void setupLayoutManager(final Context context, RecyclerView recyclerView, @DimenRes int desiredSize) {
int maxWidth = context.getResources().getDisplayMetrics().widthPixels;
ColumnInfo columnInfo = calculateColumnCount(context, maxWidth, desiredSize);
Log.d(Config.LOGTAG, "preliminary count=" + columnInfo.count);
MediaAdapter.setMediaSize(recyclerView, columnInfo.width);
recyclerView.setLayoutManager(new GridLayoutManager(context, columnInfo.count));
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
final int availableWidth = recyclerView.getMeasuredWidth();
if (availableWidth == 0) {
Log.e(Config.LOGTAG, "GridManager: available width was 0; probably because layout was hidden");
return;
}
final ColumnInfo columnInfo = calculateColumnCount(context, recyclerView.getMeasuredWidth(), desiredSize);
Log.d(Config.LOGTAG, "final count " + columnInfo.count);
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter != null && adapter.getItemCount() != 0) {
Log.e(Config.LOGTAG, "adapter already has items; just go with it now");
return;
}
setupLayoutManagerInternal(recyclerView, columnInfo);
MediaAdapter.setMediaSize(recyclerView, columnInfo.width);
}
});
}
private static void setupLayoutManagerInternal(RecyclerView recyclerView, final ColumnInfo columnInfo) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(columnInfo.count);
}
}
private static ColumnInfo calculateColumnCount(Context context, int availableWidth, @DimenRes int desiredSize) {
final float desiredWidth = context.getResources().getDimension(desiredSize);
int columns = Math.round(availableWidth / desiredWidth);
if (columns < 1) {
columns = 1;
}
final int realWidth = availableWidth / columns;
Log.d(Config.LOGTAG, "desired=" + desiredWidth + " real=" + realWidth);
return new ColumnInfo(columns, realWidth);
}
public static int getCurrentColumnCount(RecyclerView recyclerView) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
return ((GridLayoutManager) layoutManager).getSpanCount();
} else {
return 0;
}
}
public static class ColumnInfo {
private final int count;
private final int width;
private ColumnInfo(int count, int width) {
this.count = count;
this.width = width;
}
}
}
|