fix:修复Pk面包拖拽问题(适配父容器内边距)

This commit is contained in:
Max
2024-01-24 16:07:59 +08:00
parent e478c9c202
commit 5be60fe7b2

View File

@@ -3,7 +3,9 @@ package com.nnbc123.app.common.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
@@ -67,22 +69,26 @@ public class DragLayout extends RelativeLayout {
parentHeight = parent.getHeight();
}
if (left < 0) {
left = 0;
int parentPaddingLeft = getParentPaddingLeft();
if (left < parentPaddingLeft) {
left = parentPaddingLeft;
right = left + getWidth();
}
if (right > parentWidth) {
right = parentWidth;
int paddingPaddingRight = getParentPaddingRight();
if (right > (parentWidth - paddingPaddingRight)) {
right = (parentWidth - paddingPaddingRight);
left = right - getWidth();
}
if (top < 0) {
top = 0;
int parentPaddingTop = getParentPaddingTop();
if (top < parentPaddingTop) {
top = parentPaddingTop;
bottom = top + getHeight();
}
if (bottom > parentHeight) {
bottom = parentHeight;
int paddingPaddingBottom = getParentPaddingBottom();
if (bottom > (parentHeight - paddingPaddingBottom)) {
bottom = (parentHeight - paddingPaddingBottom);
top = bottom - getHeight();
}
@@ -117,8 +123,8 @@ public class DragLayout extends RelativeLayout {
}
if (layoutParams != null) {
layoutParams.leftMargin = left;
layoutParams.topMargin = top;
layoutParams.leftMargin = left - getParentPaddingLeft();
layoutParams.topMargin = top - getParentPaddingTop();
layoutParams.width = getWidth();
layoutParams.height = getHeight();
setLayoutParams(layoutParams);
@@ -150,22 +156,26 @@ public class DragLayout extends RelativeLayout {
parentHeight = parent.getHeight();
}
if (left < 0) {
left = 0;
int parentPaddingLeft = getParentPaddingLeft();
if (left < parentPaddingLeft) {
left = parentPaddingLeft;
right = left + getWidth();
}
if (right > parentWidth) {
right = parentWidth;
int paddingPaddingRight = getParentPaddingRight();
if (right > (parentWidth - paddingPaddingRight)) {
right = (parentWidth - paddingPaddingRight);
left = right - getWidth();
}
if (top < 0) {
top = 0;
int parentPaddingTop = getParentPaddingTop();
if (top < parentPaddingTop) {
top = parentPaddingTop;
bottom = top + getHeight();
}
if (bottom > parentHeight) {
bottom = parentHeight;
int paddingPaddingBottom = getParentPaddingBottom();
if (bottom > (parentHeight - paddingPaddingBottom)) {
bottom = (parentHeight - paddingPaddingBottom);
top = bottom - getHeight();
}
@@ -192,4 +202,36 @@ public class DragLayout extends RelativeLayout {
return super.performClick();
}
protected int getParentPaddingLeft() {
ViewParent viewParent = getParent();
if (viewParent instanceof View) {
return ((View) viewParent).getPaddingLeft();
}
return 0;
}
protected int getParentPaddingRight() {
ViewParent viewParent = getParent();
if (viewParent instanceof View) {
return ((View) viewParent).getPaddingRight();
}
return 0;
}
protected int getParentPaddingTop() {
ViewParent viewParent = getParent();
if (viewParent instanceof View) {
return ((View) viewParent).getPaddingTop();
}
return 0;
}
protected int getParentPaddingBottom() {
ViewParent viewParent = getParent();
if (viewParent instanceof View) {
return ((View) viewParent).getPaddingBottom();
}
return 0;
}
}