| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice.navigationbar; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.view.Gravity; |
| 23 |
import android.view.View; |
| 24 |
import android.view.ViewGroup; |
| 25 |
import android.widget.LinearLayout; |
| 26 |
import android.widget.RelativeLayout; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ReverseLinearLayout extends LinearLayout { |
| 35 |
|
| 36 |
|
| 37 |
private boolean mIsLayoutReverse; |
| 38 |
|
| 39 |
|
| 40 |
private boolean mIsAlternativeOrder; |
| 41 |
|
| 42 |
ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 43 |
super(context, attrs); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
protected void onFinishInflate() { |
| 48 |
super.onFinishInflate(); |
| 49 |
updateOrder(); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void addView(View child) { |
| 54 |
reverseParams(child.getLayoutParams(), child, mIsLayoutReverse); |
| 55 |
if (mIsLayoutReverse) { |
| 56 |
super.addView(child, 0); |
| 57 |
} else { |
| 58 |
super.addView(child); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void addView(View child, ViewGroup.LayoutParams params) { |
| 64 |
reverseParams(params, child, mIsLayoutReverse); |
| 65 |
if (mIsLayoutReverse) { |
| 66 |
super.addView(child, 0, params); |
| 67 |
} else { |
| 68 |
super.addView(child, params); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onRtlPropertiesChanged(int layoutDirection) { |
| 74 |
super.onRtlPropertiesChanged(layoutDirection); |
| 75 |
updateOrder(); |
| 76 |
} |
| 77 |
|
| 78 |
public void setAlternativeOrder(boolean alternative) { |
| 79 |
mIsAlternativeOrder = alternative; |
| 80 |
updateOrder(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private void updateOrder() { |
| 88 |
boolean isLayoutRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
| 89 |
boolean isLayoutReverse = isLayoutRtl ^ mIsAlternativeOrder; |
| 90 |
|
| 91 |
if (mIsLayoutReverse != isLayoutReverse) { |
| 92 |
|
| 93 |
int childCount = getChildCount(); |
| 94 |
ArrayList<View> childList = new ArrayList<>(childCount); |
| 95 |
for (int i = 0; i < childCount; i++) { |
| 96 |
childList.add(getChildAt(i)); |
| 97 |
} |
| 98 |
removeAllViews(); |
| 99 |
for (int i = childCount - 1; i >= 0; i--) { |
| 100 |
final View child = childList.get(i); |
| 101 |
super.addView(child); |
| 102 |
} |
| 103 |
mIsLayoutReverse = isLayoutReverse; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private static void reverseParams(ViewGroup.LayoutParams params, View child, |
| 108 |
boolean isLayoutReverse) { |
| 109 |
if (child instanceof Reversible) { |
| 110 |
((Reversible) child).reverse(isLayoutReverse); |
| 111 |
} |
| 112 |
if (child.getPaddingLeft() == child.getPaddingRight() |
| 113 |
&& child.getPaddingTop() == child.getPaddingBottom()) { |
| 114 |
child.setPadding(child.getPaddingTop(), child.getPaddingLeft(), |
| 115 |
child.getPaddingTop(), child.getPaddingLeft()); |
| 116 |
} |
| 117 |
if (params == null) { |
| 118 |
return; |
| 119 |
} |
| 120 |
int width = params.width; |
| 121 |
params.width = params.height; |
| 122 |
params.height = width; |
| 123 |
} |
| 124 |
|
| 125 |
interface Reversible { |
| 126 |
void reverse(boolean isLayoutReverse); |
| 127 |
} |
| 128 |
|
| 129 |
public static class ReverseRelativeLayout extends RelativeLayout implements Reversible { |
| 130 |
|
| 131 |
ReverseRelativeLayout(Context context) { |
| 132 |
super(context); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void reverse(boolean isLayoutReverse) { |
| 137 |
updateGravity(isLayoutReverse); |
| 138 |
reverseGroup(this, isLayoutReverse); |
| 139 |
} |
| 140 |
|
| 141 |
private int mDefaultGravity = Gravity.NO_GRAVITY; |
| 142 |
public void setDefaultGravity(int gravity) { |
| 143 |
mDefaultGravity = gravity; |
| 144 |
} |
| 145 |
|
| 146 |
public void updateGravity(boolean isLayoutReverse) { |
| 147 |
|
| 148 |
if (mDefaultGravity != Gravity.TOP && mDefaultGravity != Gravity.BOTTOM) return; |
| 149 |
|
| 150 |
|
| 151 |
int gravityToApply = mDefaultGravity; |
| 152 |
if (isLayoutReverse) { |
| 153 |
gravityToApply = mDefaultGravity == Gravity.TOP ? Gravity.BOTTOM : Gravity.TOP; |
| 154 |
} |
| 155 |
|
| 156 |
if (getGravity() != gravityToApply) setGravity(gravityToApply); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private static void reverseGroup(ViewGroup group, boolean isLayoutReverse) { |
| 161 |
for (int i = 0; i < group.getChildCount(); i++) { |
| 162 |
final View child = group.getChildAt(i); |
| 163 |
reverseParams(child.getLayoutParams(), child, isLayoutReverse); |
| 164 |
|
| 165 |
|
| 166 |
if (child instanceof ViewGroup) { |
| 167 |
reverseGroup((ViewGroup) child, isLayoutReverse); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |