Skip to content

Commit

Permalink
1. 横向Attach弹窗和气泡弹窗宽度限制
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Nov 22, 2022
1 parent cb7abfb commit 620c6c9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/lxj/xpopupdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void onCreate(Bundle savedInstanceState) {
+ " statusHeight: " + XPopupUtils.getStatusBarHeight(getWindow())
+ " navHeight: " + XPopupUtils.getNavBarHeight(getWindow());
// + " hasNav: " + XPopupUtils.isNavBarVisible(getWindow());
Log.e("tag", str);
Log.d("tag", str);
}

class MainAdapter extends FragmentPagerAdapter {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/custom_attach_popup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="?android:attr/selectableItemBackground"
android:text="评论"
android:text="评论评论评论评论评论评论评论评论评论评论评评论评论评"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2 changes: 0 additions & 2 deletions library/src/main/java/com/lxj/xpopup/core/BasePopupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public BasePopupView(@NonNull Context context) {
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
setId(View.generateViewId());
View contentView = LayoutInflater.from(context).inflate(getInnerLayoutId(), this, false);
// 事先隐藏,等测量完毕恢复,避免影子跳动现象。
contentView.setAlpha(0);
addView(contentView);
}
Expand Down Expand Up @@ -218,7 +217,6 @@ protected void doMeasure(){
params.height = activityContent.getHeight();
}
params.leftMargin = popupInfo!=null && popupInfo.isViewMode ? activityContent.getLeft():0;
params.topMargin = activityContent.getTop();
setLayoutParams(params);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ protected void initPopupContent() {
bubbleContainer.setShadowRadius(XPopupUtils.dp2px(getContext(), 0f));
defaultOffsetY = popupInfo.offsetY;
defaultOffsetX = popupInfo.offsetX;
// bubbleContainer.setTranslationX(popupInfo.offsetX);
// bubbleContainer.setTranslationY(popupInfo.offsetY);
XPopupUtils.applyPopupSize((ViewGroup) getPopupContentView(), getMaxWidth(), getMaxHeight(),
getPopupWidth(),getPopupHeight(), new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import android.content.Context;
import android.graphics.Rect;

import android.view.ViewGroup;
import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.animator.ScrollScaleAnimator;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.enums.PopupPosition;
import com.lxj.xpopup.util.XPopupUtils;
import com.lxj.xpopup.widget.BubbleLayout;
Expand All @@ -32,26 +28,38 @@ protected void initPopupContent() {
/**
* 执行附着逻辑
*/
float translationX = 0, translationY = 0;
public void doAttach() {
final boolean isRTL = XPopupUtils.isLayoutRtl(getContext());
float translationX = 0, translationY = 0;
int w = getPopupContentView().getMeasuredWidth();
int h = getPopupContentView().getMeasuredHeight();
// int w = getPopupContentView().getMeasuredWidth();
// int h = getPopupContentView().getMeasuredHeight();
//0. 判断是依附于某个点还是某个View
if (popupInfo.touchPoint != null) {
if(XPopup.longClickPoint!=null) popupInfo.touchPoint = XPopup.longClickPoint;
// 依附于指定点
popupInfo.touchPoint.x -= getActivityContentLeft();
isShowLeft = popupInfo.touchPoint.x > XPopupUtils.getAppWidth(getContext()) / 2;

// translationX: 在左边就和点左边对齐,在右边就和其右边对齐
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x+defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (popupInfo.touchPoint.x - w - defaultOffsetX) : (popupInfo.touchPoint.x + defaultOffsetX);
isShowLeft = popupInfo.touchPoint.x > XPopupUtils.getAppWidth(getContext()) / 2f;
ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
int maxWidth = (int) (XPopupUtils.getAppWidth(getContext()) - popupInfo.touchPoint.x - overflow);
if (getPopupContentView().getMeasuredWidth() > maxWidth) {
params.width = Math.max(maxWidth, getPopupWidth());
}
translationY = popupInfo.touchPoint.y - h * .5f + defaultOffsetY;
getPopupContentView().setLayoutParams(params);
getPopupContentView().post(new Runnable() {
@Override
public void run() {
if(popupInfo==null) return;
// translationX: 在左边就和点左边对齐,在右边就和其右边对齐
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x+defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (popupInfo.touchPoint.x - getPopupContentView().getMeasuredWidth() - defaultOffsetX) : (popupInfo.touchPoint.x + defaultOffsetX);
}
translationY = popupInfo.touchPoint.y - getPopupContentView().getMeasuredHeight() * .5f + defaultOffsetY;
doBubble();
}
});
} else {
// 依附于指定View
//1. 获取atView在屏幕上的位置
Expand All @@ -60,16 +68,30 @@ public void doAttach() {
rect.right -= getActivityContentLeft();

int centerX = (rect.left + rect.right) / 2;

isShowLeft = centerX > XPopupUtils.getAppWidth(getContext()) / 2;
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-rect.left + defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-rect.right-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (rect.left - w - defaultOffsetX) : (rect.right + defaultOffsetX);
ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
int maxWidth = isShowLeft ? (XPopupUtils.getAppWidth(getContext()) - rect.left - overflow) : (XPopupUtils.getAppWidth(getContext()) - rect.right - overflow);
if (getPopupContentView().getMeasuredWidth() > maxWidth) {
params.width = Math.max(maxWidth, getPopupWidth());
}
translationY = rect.top + (rect.height()-h)/2f + defaultOffsetY;
getPopupContentView().setLayoutParams(params);
getPopupContentView().post(new Runnable() {
@Override
public void run() {
isShowLeft = centerX > XPopupUtils.getAppWidth(getContext()) / 2;
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-rect.left + defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-rect.right-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (rect.left - getPopupContentView().getMeasuredWidth() - defaultOffsetX) : (rect.right + defaultOffsetX);
}
translationY = rect.top + (rect.height()-getPopupContentView().getMeasuredHeight() - bubbleContainer.getShadowRadius()*2)/2f + defaultOffsetY;
doBubble();
}
});
}
}

private void doBubble(){
//设置气泡相关
if(isShowLeftToTarget()){
bubbleContainer.setLook(BubbleLayout.Look.RIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import android.content.Context;
import android.graphics.Rect;
import android.view.ViewGroup;
import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.animator.ScrollScaleAnimator;
Expand All @@ -28,12 +28,14 @@ protected void initPopupContent() {
defaultOffsetX = popupInfo.offsetX == 0 ? XPopupUtils.dp2px(getContext(), 2) : popupInfo.offsetX;
}

float translationX = 0, translationY = 0;
/**
* 执行附着逻辑
*/
@Override
public void doAttach() {
if(popupInfo==null)return;
final boolean isRTL = XPopupUtils.isLayoutRtl(getContext());
float translationX = 0, translationY = 0;
int w = getPopupContentView().getMeasuredWidth();
int h = getPopupContentView().getMeasuredHeight();
//0. 判断是依附于某个点还是某个View
Expand All @@ -42,36 +44,60 @@ public void doAttach() {
// 依附于指定点
popupInfo.touchPoint.x -= getActivityContentLeft();
isShowLeft = popupInfo.touchPoint.x > XPopupUtils.getAppWidth(getContext()) / 2f;

// translationX: 在左边就和点左边对齐,在右边就和其右边对齐
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x+defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (popupInfo.touchPoint.x - w - defaultOffsetX) : (popupInfo.touchPoint.x + defaultOffsetX);
//限制最大宽高
ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
int maxWidth = (int) (XPopupUtils.getAppWidth(getContext()) - popupInfo.touchPoint.x - overflow);
if (getPopupContentView().getMeasuredWidth() > maxWidth) {
params.width = Math.max(maxWidth, getPopupWidth());
}
translationY = popupInfo.touchPoint.y - h * .5f + defaultOffsetY;
getPopupContentView().setLayoutParams(params);
getPopupContentView().post(new Runnable() {
@Override
public void run() {
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x+defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-popupInfo.touchPoint.x-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (popupInfo.touchPoint.x - w - defaultOffsetX) : (popupInfo.touchPoint.x + defaultOffsetX);
}
translationY = popupInfo.touchPoint.y - h * .5f + defaultOffsetY;
getPopupContentView().setTranslationX(translationX);
getPopupContentView().setTranslationY(translationY);
initAndStartAnimation();
}
});
} else {
// 依附于指定View
//1. 获取atView在屏幕上的位置
Rect rect = popupInfo.getAtViewRect();
rect.left -= getActivityContentLeft();
rect.right -= getActivityContentLeft();
int centerX = (rect.left + rect.right) / 2;

isShowLeft = centerX > XPopupUtils.getAppWidth(getContext()) / 2;
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-rect.left + defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-rect.right-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (rect.left - w - defaultOffsetX) : (rect.right + defaultOffsetX);
//限制最大宽高
ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
int maxWidth = isShowLeft ? (XPopupUtils.getAppWidth(getContext()) - rect.left - overflow) : (XPopupUtils.getAppWidth(getContext()) - rect.right - overflow);
if (getPopupContentView().getMeasuredWidth() > maxWidth) {
params.width = Math.max(maxWidth, getPopupWidth());
}
translationY = rect.top + (rect.height()-h)/2f + defaultOffsetY;
getPopupContentView().setLayoutParams(params);
getPopupContentView().post(new Runnable() {
@Override
public void run() {
isShowLeft = centerX > XPopupUtils.getAppWidth(getContext()) / 2;
if(isRTL){
translationX = isShowLeft ? -(XPopupUtils.getAppWidth(getContext())-rect.left + defaultOffsetX)
: -(XPopupUtils.getAppWidth(getContext())-rect.right-getPopupContentView().getMeasuredWidth()-defaultOffsetX);
}else {
translationX = isShowLeftToTarget() ? (rect.left - w - defaultOffsetX) : (rect.right + defaultOffsetX);
}
translationY = rect.top + (rect.height()-h)/2f + defaultOffsetY;
getPopupContentView().setTranslationX(translationX);
getPopupContentView().setTranslationY(translationY);
initAndStartAnimation();
}
});
}
//
getPopupContentView().setTranslationX(translationX);
getPopupContentView().setTranslationY(translationY);
initAndStartAnimation();

}

private boolean isShowLeftToTarget() {
Expand Down

0 comments on commit 620c6c9

Please sign in to comment.