forked from ea167/android-layout-wrap-content-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutWrapContentUpdater.java
125 lines (108 loc) · 5.2 KB
/
LayoutWrapContentUpdater.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package your.name.here.util;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
/**
* README: Must be called on UI-Thread (main thread), so post to a handler if needed
*
* NOTES:
* - To enable easier debugging, tag your nodes with android:tag="Name-of-my-node"
* - Avoid using wrap_content on ScrollView!
* - If you have center or right/bottom gravity, you should re-layout all nodes, not only the wrap_content: just call the method with the boolean set to true
*
* @author Eric, April 2014
*/
public class LayoutWrapContentUpdater
{
public static final String TAG = LayoutWrapContentUpdater.class.getName();
/**
* Does what a proper requestLayout() should do about layout_width or layout_height = "wrap_content"
*
* Warning: if the subTreeRoot itself has a "wrap_content" layout param, the size will be computed without boundaries maximum size.
* If you do have limits, consider either passing the parent, or calling the method with the size parameters (View.MeasureSpec)
*
* @param subTreeRoot root of the sub tree you want to recompute
*/
public static final void wrapContentAgain( ViewGroup subTreeRoot )
{
wrapContentAgain( subTreeRoot, false, MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED );
}
/** Same but allows re-layout of all views, not only those with "wrap_content". Necessary for "center", "right", "bottom",... */
public static final void wrapContentAgain( ViewGroup subTreeRoot, boolean relayoutAllNodes )
{
wrapContentAgain( subTreeRoot, relayoutAllNodes, MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED );
}
/**
* Same as previous, but with given size in case subTreeRoot itself has layout_width or layout_height = "wrap_content"
*/
public static void wrapContentAgain( ViewGroup subTreeRoot, boolean relayoutAllNodes,
int subTreeRootWidthMeasureSpec, int subTreeRootHeightMeasureSpec )
{
Log.d(TAG, "+++ LayoutWrapContentUpdater wrapContentAgain on subTreeRoot=["+ subTreeRoot +"], with w="
+ subTreeRootWidthMeasureSpec +" and h="+ subTreeRootHeightMeasureSpec );
assert( "main".equals( Thread.currentThread().getName() ) );
if (subTreeRoot == null)
return;
LayoutParams layoutParams = subTreeRoot.getLayoutParams();
// --- First, we force measure on the subTree
int widthMeasureSpec = subTreeRootWidthMeasureSpec;
// When LayoutParams.MATCH_PARENT and Width > 0, we apply measured width to avoid getting dimensions too big
if ( layoutParams.width != LayoutParams.WRAP_CONTENT && subTreeRoot.getWidth() > 0 )
widthMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getWidth(), MeasureSpec.EXACTLY );
int heightMeasureSpec = subTreeRootHeightMeasureSpec;
// When LayoutParams.MATCH_PARENT and Height > 0, we apply measured height to avoid getting dimensions too big
if ( layoutParams.height != LayoutParams.WRAP_CONTENT && subTreeRoot.getHeight() > 0 )
heightMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getHeight(), MeasureSpec.EXACTLY );
// This measure recursively the whole sub-tree
subTreeRoot.measure( widthMeasureSpec, heightMeasureSpec );
// --- Then recurse on all children to correct the sizes
recurseWrapContent( subTreeRoot, relayoutAllNodes );
// --- RequestLayout to finish properly
subTreeRoot.requestLayout();
return;
}
/**
* Internal method to recurse on view tree. Tag you View nodes in XML layouts to read the logs more easily
*/
private static void recurseWrapContent( View nodeView, boolean relayoutAllNodes )
{
// Does not recurse when visibility GONE
if ( nodeView.getVisibility() == View.GONE ) {
// nodeView.layout( nodeView.getLeft(), nodeView.getTop(), 0, 0 ); // No need
return;
}
LayoutParams layoutParams = nodeView.getLayoutParams();
boolean isWrapWidth = ( layoutParams.width == LayoutParams.WRAP_CONTENT ) || relayoutAllNodes;
boolean isWrapHeight = ( layoutParams.height == LayoutParams.WRAP_CONTENT ) || relayoutAllNodes;
if ( isWrapWidth || isWrapHeight ) {
boolean changed = false;
int right = nodeView.getRight();
int bottom = nodeView.getBottom();
if ( isWrapWidth && nodeView.getMeasuredWidth() > 0 ) {
right = nodeView.getLeft() + nodeView.getMeasuredWidth();
changed = true;
Log.v(TAG, "+++ LayoutWrapContentUpdater recurseWrapContent set Width to "+ nodeView.getMeasuredWidth() +" of node Tag="+ nodeView.getTag() +" ["+ nodeView +"]");
}
if ( isWrapHeight && nodeView.getMeasuredHeight() > 0 ) {
bottom = nodeView.getTop() + nodeView.getMeasuredHeight();
changed = true;
Log.v(TAG, "+++ LayoutWrapContentUpdater recurseWrapContent set Height to "+ nodeView.getMeasuredHeight() +" of node Tag="+ nodeView.getTag() +" ["+ nodeView +"]");
}
if (changed) {
nodeView.layout( nodeView.getLeft(), nodeView.getTop(), right, bottom );
// FIXME: Adjust left & top position when gravity = "center" / "bottom" / "right"
}
}
// --- Recurse
if ( nodeView instanceof ViewGroup ) {
ViewGroup nodeGroup = (ViewGroup)nodeView;
for (int i = 0; i < nodeGroup.getChildCount(); i++) {
recurseWrapContent( nodeGroup.getChildAt(i), relayoutAllNodes );
}
}
return;
}
// End of class
}