From b2504a79125db92f800f64dd4a227733542a45fe Mon Sep 17 00:00:00 2001
From: Gabriel Machado <97042217+GabrielBRDeveloper@users.noreply.github.com>
Date: Fri, 5 Jul 2024 10:32:31 -0400
Subject: [PATCH] Implement: ViewGroup.addView(child, params)
---
res/ex-layout/main.xml | 93 ++++++-------
src/br/nullexcept/mux/test/Example.java | 26 ++--
src/br/nullexcept/mux/view/View.java | 9 ++
src/br/nullexcept/mux/view/ViewGroup.java | 7 +-
.../nullexcept/mux/widget/AbsoluteLayout.java | 13 +-
src/br/nullexcept/mux/widget/FrameLayout.java | 13 +-
.../nullexcept/mux/widget/LinearLayout.java | 125 ++++++++++++++----
7 files changed, 183 insertions(+), 103 deletions(-)
diff --git a/res/ex-layout/main.xml b/res/ex-layout/main.xml
index 5c96ac8..8d8bdbc 100644
--- a/res/ex-layout/main.xml
+++ b/res/ex-layout/main.xml
@@ -1,65 +1,60 @@
-
-
+
+
+ height="wrap_content"
+ orientation="horizontal"
+ padding="10dp"
+ background="#555"
+ gravity="center">
+
+
+
+ height="wrap_content"
+ paddingLeft="20dp"
+ background="#F00">
-
-
+ textSize="19dp"
+ text="Title"/>
-
-
+ textSize="14dp"
+ textStyle="bold"
+ textColor="?colorPrimary"
+ paddingTop="4dp"
+ paddingBottom="3dp"
+ text="Title"/>
-
-
+ textSize="13dp"
+ alpha="0.5"
+ paddingTop="4dp"
+ paddingBottom="3dp"
+ text="Title"/>
+
-
+
-
-
-
-
-
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/src/br/nullexcept/mux/test/Example.java b/src/br/nullexcept/mux/test/Example.java
index 870b9ea..ebf64ac 100644
--- a/src/br/nullexcept/mux/test/Example.java
+++ b/src/br/nullexcept/mux/test/Example.java
@@ -9,6 +9,7 @@
import br.nullexcept.mux.view.View;
public class Example extends Activity {
+ private static int layer = 0;
public static void main(String[] args) {
Application.initialize(Project.build(new Launch<>(Example.class), "br.nullexcept.test-master-ui"));
}
@@ -19,27 +20,16 @@ public static void main(String[] args) {
@Override
public void onCreate() {
super.onCreate();
- setContentView("@ex-layout/main");
- animate = findViewById("animate");
- update();
- }
- void update() {
- animate.getTransition().getTranslate().set((clamp(x, 100))-50,0);
- animate.getTransition().getScale().set(clamp(z, 200)/100.0f, 1.0f);
- animate.getTransition().setRotation(clamp(z+126, 360));
- x++;
- z++;
- animate.invalidate();
- postDelayed(this::update, 15);
+ if (layer == 0) {
+ postDelayed(() -> {
+ // startActivity(new Launch<>(Example.class).addFlags(FLAG_ACTIVITY_NEW_WINDOW));
+ }, 1);
+ }
+ layer++;
+ setContentView("@ex-layout/main");
}
- public int clamp(int value, int max) {
- int rest = value % max;
- int div = value / max;
-
- return div % 2 == 0 ? rest : (max - rest);
- }
@Override
public void onDestroy() {
diff --git a/src/br/nullexcept/mux/view/View.java b/src/br/nullexcept/mux/view/View.java
index c80c483..d636687 100644
--- a/src/br/nullexcept/mux/view/View.java
+++ b/src/br/nullexcept/mux/view/View.java
@@ -593,6 +593,15 @@ public Object getTag() {
return tag;
}
+ @Override
+ public String toString() {
+ return getClass().getSimpleName()+" { " +
+ "id=" + id +";"+
+ "tag=" + tag +";" +
+ "attached="+ (getViewRoot() != null) +";"+
+ "}";
+ }
+
public interface OnClickListener {
void onClick(View view);
}
diff --git a/src/br/nullexcept/mux/view/ViewGroup.java b/src/br/nullexcept/mux/view/ViewGroup.java
index 7856b56..ca49d4c 100644
--- a/src/br/nullexcept/mux/view/ViewGroup.java
+++ b/src/br/nullexcept/mux/view/ViewGroup.java
@@ -243,7 +243,8 @@ public T getChildAt(int index){
return (T) children.get(index);
}
- public void addChild(View view){
+ public void addChild(View view, LayoutParams params) {
+ view.setLayoutParams(params == null ? new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) : params);
children.add(view);
view.setParent(this);
onChildAdded(view);
@@ -252,6 +253,10 @@ public void addChild(View view){
notifyTreeChanged();
}
+ public void addChild(View view){
+ addChild(view, view.getLayoutParams());
+ }
+
protected void requestFocus(View focused){
if (getParent() != null){
getParent().requestFocus(focused);
diff --git a/src/br/nullexcept/mux/widget/AbsoluteLayout.java b/src/br/nullexcept/mux/widget/AbsoluteLayout.java
index 4cff1a3..439f0e6 100644
--- a/src/br/nullexcept/mux/widget/AbsoluteLayout.java
+++ b/src/br/nullexcept/mux/widget/AbsoluteLayout.java
@@ -18,13 +18,16 @@ public AbsoluteLayout(Context context, AttributeList attrs) {
}
@Override
- public void addChild(View view) {
- if (!(view.getLayoutParams() instanceof LayoutParams)){
- view.setLayoutParams(new LayoutParams(view.getLayoutParams().width, view.getLayoutParams().height));
+ public void addChild(View view, ViewGroup.LayoutParams params) {
+ if (params == null) {
+ params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
- super.addChild(view);
+ if (!(params instanceof LayoutParams)) {
+ params = new LayoutParams(params.width, params.height);
+ }
+ super.addChild(view, params);
}
-
+
@Override
protected Point getChildLocation(View view) {
LayoutParams params = (LayoutParams) view.getLayoutParams();
diff --git a/src/br/nullexcept/mux/widget/FrameLayout.java b/src/br/nullexcept/mux/widget/FrameLayout.java
index f26654d..c4f2d93 100644
--- a/src/br/nullexcept/mux/widget/FrameLayout.java
+++ b/src/br/nullexcept/mux/widget/FrameLayout.java
@@ -39,11 +39,14 @@ protected Point getChildLocation(View view) {
}
@Override
- public void addChild(View view) {
- LayoutParams params = new LayoutParams(0,0);
- params.from(view.getLayoutParams());
- view.setLayoutParams(params);
- super.addChild(view);
+ public void addChild(View view, ViewGroup.LayoutParams params) {
+ if (params == null) {
+ params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
+ }
+ LayoutParams newParams = new LayoutParams(0,0);
+ newParams.from(params);
+
+ super.addChild(view, newParams);
}
public static class LayoutParams extends MarginLayoutParams {
diff --git a/src/br/nullexcept/mux/widget/LinearLayout.java b/src/br/nullexcept/mux/widget/LinearLayout.java
index 743f6ef..fcd5a4d 100644
--- a/src/br/nullexcept/mux/widget/LinearLayout.java
+++ b/src/br/nullexcept/mux/widget/LinearLayout.java
@@ -7,6 +7,8 @@
import br.nullexcept.mux.res.AttributeList;
import br.nullexcept.mux.view.*;
+import java.util.List;
+
public class LinearLayout extends ViewGroup {
public static int ORIENTATION_HORIZONTAL = 1;
public static int ORIENTATION_VERTICAL = 0;
@@ -25,7 +27,7 @@ public LinearLayout(Context context, AttributeList attrs) {
attrs = initialAttributes();
attrs.searchDimension(AttrList.dividerSize, value -> dividerSize = value.intValue());
attrs.searchRaw(AttrList.orientation, value -> {
- if ("horizontal".equals(value)){
+ if ("horizontal".equals((value+"").toLowerCase())){
setOrientation(ORIENTATION_HORIZONTAL);
} else {
setOrientation(ORIENTATION_VERTICAL);
@@ -33,46 +35,119 @@ public LinearLayout(Context context, AttributeList attrs) {
});
}
-
@Override
- public void addChild(View view) {
- LayoutParams params = new LayoutParams(0,0);
- params.from(view.getLayoutParams());
- view.setLayoutParams(params);
- super.addChild(view);
+ public void addChild(View view, ViewGroup.LayoutParams params) {
+ if (params == null) {
+ params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
+ }
+
+ LayoutParams newParams = new LayoutParams(0,0);
+ newParams.from(params);
+ view.setLayoutParams(newParams);
+
+ super.addChild(view, newParams);
}
@Override
protected Point getChildLocation(View view) {
- Point point = new Point(getPaddingLeft(), getPaddingTop());
- int mh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
- int mw = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
- int index = getChildIndex(view);
- Gravity.applyGravity(getGravity(),view.getMeasuredWidth(),view.getMeasuredHeight(),mw,mh,rect);
+ return orientation == ORIENTATION_VERTICAL ? getChildLocationVertical(view) : getChildLocationHorizontal(view);
+ }
- for (int i = 0; i < index; i++){
+ private Size measureHorizontal() {
+ Size size = new Size();
+ for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
+ if (child.getVisibility() == VISIBILITY_GONE) continue;
LayoutParams params = (LayoutParams) child.getLayoutParams();
- if (orientation == ORIENTATION_HORIZONTAL){
- point.x += child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight() + dividerSize;
- } else {
- point.y += child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom() + dividerSize;
- }
+ size.width += child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight();
+ size.height = Math.max(child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom(), size.height);
+ if (i != getChildrenCount()) size.width += dividerSize;
}
+ return size;
+ }
+
+ private Size measureVertical() {
+ Size size = new Size();
+ for (int i = 0; i < getChildrenCount(); i++) {
+ View child = getChildAt(i);
+ if (child.getVisibility() == VISIBILITY_GONE) continue;
+ LayoutParams params = (LayoutParams) child.getLayoutParams();
+ size.width = Math.max(size.width, child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight());
+ size.height += child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom();
+ if (i != getChildrenCount()) size.height += dividerSize;
+ }
+ return size;
+ }
+
+ @Override
+ protected Size onMeasureContent(int parentWidth, int parentHeight) {
+ return super.onMeasureContent(parentWidth, parentHeight);
+ }
+ private Point getChildLocationVertical(View view) {
+ if (view.getVisibility() == VISIBILITY_GONE)
+ return new Point(0,0);
+ Size wrapSize = measureVertical();
LayoutParams params = (LayoutParams) view.getLayoutParams();
- point.x += params.getMarginLeft() - params.getMarginRight();
- point.y += params.getMarginTop() - params.getMarginBottom();
+ int w = Math.max(0, getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - params.getMarginRight());
+ int h = Math.max(0, getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - params.getMarginBottom());
- if (orientation == ORIENTATION_HORIZONTAL) {
- point.y += rect.top;
- } else {
- point.x += rect.left;
+ int posX = Gravity.apply(Gravity.horizontal(getGravity()),w,wrapSize.width);
+ int posY = Gravity.apply(Gravity.vertical(getGravity()),h, wrapSize.height);
+
+ posX = Math.max(0, posX);
+ posY = Math.max(0, posY);
+
+ Point pos = new Point(0, posY);
+ for (int i = 0; i < getChildrenCount(); i++) {
+ View child = getChildAt(i);
+ if (child.equals(view)) break;
+ if (child.getVisibility() == VISIBILITY_GONE) continue;
+ LayoutParams childParams = (LayoutParams) child.getLayoutParams();
+ pos.y += childParams.getMarginTop() + childParams.getMarginBottom() + child.getMeasuredHeight();
+ }
+
+ pos.x = posX + Gravity.apply(Gravity.horizontal(getGravity()),wrapSize.width,view.getMeasuredWidth());
+ pos.x += params.getMarginLeft();
+
+ pos.y += getPaddingTop();
+ pos.x += getPaddingLeft();
+
+ return pos;
+ }
+
+ private Point getChildLocationHorizontal(View view) {
+ if (view.getVisibility() == VISIBILITY_GONE)
+ return new Point(0,0);
+ Size wrapSize = measureHorizontal();
+
+ LayoutParams params = (LayoutParams) view.getLayoutParams();
+
+ int w = Math.max(0, getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - params.getMarginRight());
+ int h = Math.max(0, getMeasuredHeight() - getPaddingBottom() - getPaddingTop() - params.getMarginBottom());
+
+ int posX = Gravity.apply(Gravity.horizontal(getGravity()),w,wrapSize.width);
+ int posY = Gravity.apply(Gravity.vertical(getGravity()),h, wrapSize.height);
+
+ posX = Math.max(0, posX);
+ posY = Math.max(0, posY);
+
+ Point pos = new Point(posX, 0);
+ for (int i = 0; i < getChildrenCount(); i++) {
+ View child = getChildAt(i);
+ if (child.equals(view)) break;
+ if (child.getVisibility() == VISIBILITY_GONE) continue;
+ LayoutParams childParams = (LayoutParams) child.getLayoutParams();
+ pos.x += dividerSize;
+ pos.x += childParams.getMarginLeft() + childParams.getMarginRight() + child.getMeasuredWidth();
}
+ pos.y = posY + Gravity.apply(Gravity.vertical(getGravity()),wrapSize.height,view.getMeasuredHeight());
+ pos.y += params.getMarginTop() + getPaddingTop();
+ pos.x += getPaddingLeft();
- return point;
+ return pos;
}
public void setOrientation(int orientation) {