diff --git a/library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java b/library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java index 7d55e7e0ac61f8e0df69affcf0bf608cd32e2813..435fbcc446749c540b6f3175932404fb7956731b 100644 --- a/library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java +++ b/library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java @@ -7,6 +7,7 @@ import ohos.agp.animation.Animator; import ohos.agp.animation.AnimatorValue; import ohos.agp.components.AttrSet; import ohos.agp.components.Component; +import ohos.agp.components.ComponentContainer; import ohos.agp.components.DragInfo; import ohos.agp.components.StackLayout; import ohos.agp.utils.Point; @@ -16,11 +17,11 @@ import ohos.app.Context; * Description: 智能的拖拽布局(水平) * Create by dance, at 2018/12/23 */ -public class PopupDrawerLayout extends StackLayout implements Component.BindStateChangedListener, Component.LayoutRefreshedListener { +public class PopupDrawerLayout extends StackLayout implements Component.BindStateChangedListener, ComponentContainer.ArrangeListener { private static final String TAG = "SmartDragLayout"; private Component placeHolder; - private Component mChild; + private Component mChild; public PopupPosition position = PopupPosition.Left; ShadowBgAnimator bgAnimator = new ShadowBgAnimator(); boolean enableDrag = true; // 是否启用手势拖拽 @@ -39,30 +40,37 @@ public class PopupDrawerLayout extends StackLayout implements Component.BindStat super(context, attrSet, styleName); setBindStateChangedListener(this); setDraggedListener(DRAG_HORIZONTAL, mDragListener); - setLayoutRefreshedListener(this); + setArrangeListener(this); } int parentWidth = 0; int parentHeight = 0; int childWidth = 0; int childHeight = 0; + private boolean hasLayout = false; @Override - public void onRefreshed(Component component) { + public boolean onArrange(int left, int top, int right, int bottom) { if (placeHolder == null) { placeHolder = getComponentAt(0); mChild = getComponentAt(1); } - parentWidth = getWidth(); - parentHeight = getHeight(); - childWidth = mChild.getWidth(); - childHeight = mChild.getHeight(); - placeHolder.setComponentPosition(0, 0, parentWidth, parentHeight); - if (position == PopupPosition.Left) { - mChild.setComponentPosition(0, 0, childWidth, childHeight); + parentWidth = getEstimatedWidth(); + parentHeight = getEstimatedHeight(); + childWidth = mChild.getEstimatedWidth(); + childHeight = mChild.getEstimatedHeight(); + placeHolder.arrange(0, 0, parentWidth, parentHeight); + if (!hasLayout) { + if (position == PopupPosition.Left) { + mChild.arrange(0, 0, childWidth, childHeight); + } else { + mChild.arrange(parentWidth - childWidth, 0, parentWidth, childHeight); + } + hasLayout = true; } else { - mChild.setComponentPosition(parentWidth - childWidth, 0, parentWidth, childHeight); + mChild.arrange(mChild.getLeft(), mChild.getTop(), mChild.getRight(), mChild.getEstimatedHeight()); } + return true; } DraggedListener mDragListener = new DraggedListener() { @@ -84,6 +92,12 @@ public class PopupDrawerLayout extends StackLayout implements Component.BindStat int tx = (int) (dragInfo.updatePoint.getPointX() - startPoint.getPointX()); tx = tx > childWidth - 1 ? childWidth - 1 : tx; tx = tx < -childWidth + 1 ? -childWidth + 1 : tx; + if (position == PopupPosition.Left) { + tx = tx > 0 ? 0 : tx; + } else { + tx = tx < 0 ? 0 : tx; + } + isToLeft = tx < 0; translationX(tx); } } @@ -112,12 +126,6 @@ public class PopupDrawerLayout extends StackLayout implements Component.BindStat boolean isToLeft; public void translationX(int dx) { - if (position == PopupPosition.Left) { - dx = dx > 0 ? 0 : dx; - } else { - dx = dx < 0 ? 0 : dx; - } - isToLeft = dx < 0; float fraction = dx * 1f / childHeight; if (hasShadowBg) { setBackground(ElementUtil.getShapeElement(bgAnimator.calculateBgColor(fraction))); diff --git a/library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java b/library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java index b67ce68c924a25fbd2912d3eab6d5490f143cc8c..6046228f5d2a9ba8f3ca1cb2266900aed1402920 100644 --- a/library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java +++ b/library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java @@ -4,6 +4,7 @@ import ohos.agp.animation.Animator; import ohos.agp.animation.AnimatorValue; import ohos.agp.components.AttrSet; import ohos.agp.components.Component; +import ohos.agp.components.ComponentContainer; import ohos.agp.components.DragInfo; import ohos.agp.components.StackLayout; import ohos.agp.render.Canvas; @@ -14,7 +15,7 @@ import ohos.app.Context; * Description: 智能的拖拽布局(垂直),优先滚动整体,整体滚到头,则滚动内部能滚动的View * Create by dance, at 2018/12/23 */ -public class SmartDragLayout extends StackLayout implements Component.BindStateChangedListener, Component.DrawTask { +public class SmartDragLayout extends StackLayout implements Component.BindStateChangedListener, ComponentContainer.ArrangeListener { private Component child; boolean enableDrag = true; // 是否启用手势拖拽 @@ -31,7 +32,7 @@ public class SmartDragLayout extends StackLayout implements Component.BindStateC public SmartDragLayout(Context context, AttrSet attrSet, String styleName) { super(context, attrSet, styleName); setBindStateChangedListener(this); - addDrawTask(this); + setArrangeListener(this); setDraggedListener(DRAG_VERTICAL, mDragListener); } @@ -45,11 +46,12 @@ public class SmartDragLayout extends StackLayout implements Component.BindStateC } @Override - public void onDraw(Component component, Canvas canvas) { - maxY = child.getHeight(); + public boolean onArrange(int left, int top, int right, int bottom) { + maxY = child.getEstimatedHeight(); minY = 0; - int left = getWidth() / 2 - child.getWidth() / 2; - child.setComponentPosition(left, getHeight(), left + child.getWidth(), getHeight() + maxY); + int childLeft = getEstimatedWidth() / 2 - child.getEstimatedWidth() / 2; + child.arrange(childLeft, getEstimatedHeight() - maxY, childLeft + child.getEstimatedWidth(), getEstimatedHeight()); + return false; } Component.DraggedListener mDragListener = new Component.DraggedListener() { @@ -70,6 +72,8 @@ public class SmartDragLayout extends StackLayout implements Component.BindStateC if (enableDrag) { int ty = (int) (dragInfo.updatePoint.getPointY() - startPoint.getPointY()); ty = ty > maxY - 1 ? maxY - 1 : ty; + isScrollUp = ty < 0; + ty = ty < 0 ? 0 : ty; translationY(ty); } } @@ -96,8 +100,6 @@ public class SmartDragLayout extends StackLayout implements Component.BindStateC boolean isScrollUp; public void translationY(int dy) { - dy = dy < 0 ? 0 : dy; - isScrollUp = dy < 0; float fraction = dy * 1f / maxY; if (listener != null) { listener.onDrag(dy, fraction, isScrollUp); @@ -151,6 +153,7 @@ public class SmartDragLayout extends StackLayout implements Component.BindStateC this.listener = listener; } + public interface OnCloseListener { void onClose();