Skip to content

Commit 8035876

Browse files
committed
popup 箭头靠右
1 parent 5099ec1 commit 8035876

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

app/src/main/java/com/mylhyl/circledialog/sample/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ protected void convert(BaseViewHolder helper, MySectionEntity item) {
394394
dialogFragment = new CircleDialog.Builder()
395395
.setRadius(0)
396396
.setWidth(1f)
397-
.setMaxHeight(0.8f)
397+
.setMaxHeight(0.7f)
398398
.setYoff(0)
399399
.setTitle("rvAdapter")
400400
.setSubTitle("副标题哦!")
@@ -505,7 +505,7 @@ protected void convert(BaseViewHolder helper, WeiBoItem item) {
505505
new CircleDialog.Builder()
506506
.setMaxHeight(0.7f)
507507
.configDialog(params -> params.backgroundColorPress = Color.CYAN)
508-
.setTitle("Rv Vertical")
508+
// .setTitle("Rv Vertical")
509509
.configItems(params -> params.dividerHeight = 1)
510510
.setItems(rvListForV, new LinearLayoutManager(this)
511511
, (view13, position13) ->

circledialog/src/main/java/com/mylhyl/circledialog/AbsBaseCircleDialog.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public abstract class AbsBaseCircleDialog extends DialogFragment {
6060
private int mRadius = CircleDimen.DIALOG_RADIUS;//对话框的圆角半径
6161
private float mAlpha = CircleDimen.DIALOG_ALPHA;//对话框透明度,范围:0-1;1不透明
6262
private int mX, mY, mAbsoluteWidth;
63-
private View.OnLayoutChangeListener mOnLayoutChangeListener;
6463
private WeakReference<View> mAnchor;
6564

6665
public AbsBaseCircleDialog() {
@@ -84,7 +83,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
8483
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
8584
super.onViewCreated(view, savedInstanceState);
8685
if (mMaxHeight > 0) {
87-
mOnLayoutChangeListener = new View.OnLayoutChangeListener() {
86+
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
8887
@Override
8988
public void onLayoutChange(View v, int left, int top, int right, int bottom
9089
, int oldLeft, int oldTop, int oldRight, int oldBottom) {
@@ -94,10 +93,10 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom
9493
if (height > maxHeight) {
9594
view.setLayoutParams(new FrameLayout.LayoutParams(
9695
FrameLayout.LayoutParams.MATCH_PARENT, maxHeight));
96+
view.removeOnLayoutChangeListener(this);
9797
}
9898
}
99-
};
100-
view.addOnLayoutChangeListener(mOnLayoutChangeListener);
99+
});
101100
}
102101
}
103102

@@ -134,10 +133,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
134133

135134
@Override
136135
public void onDismiss(DialogInterface dialog) {
137-
View view = getView();
138-
if (view != null && mOnLayoutChangeListener != null) {
139-
view.removeOnLayoutChangeListener(mOnLayoutChangeListener);
140-
}
141136
super.onDismiss(dialog);
142137
remove();
143138
}
@@ -171,7 +166,7 @@ private void setDialogGravity(Dialog dialog) {
171166
window.setBackgroundDrawableResource(android.R.color.transparent);
172167
WindowManager.LayoutParams wlp = window.getAttributes();
173168
DisplayMetrics dm = getDisplayMetrics();
174-
if (mAbsoluteWidth >= 0) {
169+
if (mAbsoluteWidth == 0) {
175170
wlp.width = (int) (dm.widthPixels * mWidth);//宽度按屏幕大小的百分比设置
176171
} else {
177172
wlp.width = mAbsoluteWidth;

circledialog/src/main/java/com/mylhyl/circledialog/view/BuildViewAbs.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,24 @@ protected final void buildTitleView(ViewGroup viewGroup) {
3535
}
3636

3737
protected void buildRootView() {
38+
CardView cardView = buildCardView();
39+
LinearLayout linearLayout = buildLinearLayout();
40+
cardView.addView(linearLayout);
41+
mRoot = cardView;
42+
}
43+
44+
protected CardView buildCardView() {
3845
CardView cardView = new CardView(mContext);
3946
cardView.setCardElevation(0f);
4047
cardView.setCardBackgroundColor(Color.TRANSPARENT);
4148
cardView.setRadius(mParams.dialogParams.radius);
49+
return cardView;
50+
}
4251

52+
protected LinearLayout buildLinearLayout() {
4353
mRootCardViewByLinearLayout = new LinearLayout(mContext);
4454
mRootCardViewByLinearLayout.setOrientation(LinearLayout.VERTICAL);
45-
cardView.addView(mRootCardViewByLinearLayout);
46-
47-
mRoot = cardView;
55+
return mRootCardViewByLinearLayout;
4856
}
4957

5058
@Override

circledialog/src/main/java/com/mylhyl/circledialog/view/BuildViewPopupImpl.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.graphics.drawable.Drawable;
55
import android.os.Build;
6+
import android.support.v7.widget.CardView;
67
import android.util.DisplayMetrics;
78
import android.view.View;
89
import android.widget.LinearLayout;
@@ -34,12 +35,14 @@ public ItemsView getBodyView() {
3435

3536
@Override
3637
public void buildBodyView() {
37-
buildRootView();
38+
LinearLayout rootLinearLayout = buildLinearLayout();
39+
mRoot = rootLinearLayout;
3840
if (mItemsView == null) {
3941
mParams.dialogParams.absoluteWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
4042

4143
final PopupParams popupParams = mParams.popupParams;
4244
final View arrowView = new View(mContext);
45+
mRoot.addView(arrowView);
4346

4447
final int arrowDirection = popupParams.arrowDirection;
4548
int backgroundColor = popupParams.backgroundColor != 0
@@ -50,28 +53,43 @@ public void buildBodyView() {
5053
} else {
5154
arrowView.setBackgroundDrawable(arrowDrawable);
5255
}
53-
mRootCardViewByLinearLayout.addView(arrowView);
56+
57+
CardView cardView = buildCardView();
58+
mRoot.addView(cardView);
5459

5560
mItemsView = new BodyRecyclerView(mContext, mParams.popupParams
5661
, mParams.dialogParams, mParams.rvItemListener);
57-
View itemsViewView = mItemsView.getView();
62+
final View itemsViewView = mItemsView.getView();
63+
cardView.addView(itemsViewView);
64+
5865
itemsViewView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
5966
@Override
60-
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
67+
public void onLayoutChange(View v, int left, int top, int right, int bottom
68+
, int oldLeft, int oldTop, int oldRight, int oldBottom) {
6169
mParams.dialogParams.absoluteWidth = v.getWidth();
6270
int arrowViewWidth = (int) (v.getWidth() * ARROW_WEIGHT);
63-
final LayoutParams arrowViewLayoutParams = new LayoutParams(arrowViewWidth, arrowViewWidth);
64-
// switch (arrowDirection) {
65-
// case PopupParams.DIRECTION_TOP:
66-
// if (popupParams.arrowGravity == PopupParams.GRAVITY_RIGHT) {
67-
// arrowViewLayoutParams.leftMargin = (int) (v.getWidth() * (1 - ARROW_WEIGHT));
68-
// }
69-
// break;
70-
// }
71+
LayoutParams arrowViewLayoutParams = (LayoutParams) arrowView.getLayoutParams();
72+
if (arrowViewLayoutParams == null) {
73+
arrowViewLayoutParams = new LayoutParams(arrowViewWidth, arrowViewWidth);
74+
} else {
75+
arrowViewLayoutParams.width = arrowViewWidth;
76+
arrowViewLayoutParams.height = arrowViewWidth;
77+
}
78+
if ((bottom != 0 && oldBottom != 0 && bottom == oldBottom)
79+
|| (top != 0 && oldTop != 0 && top == oldTop)) {
80+
switch (arrowDirection) {
81+
case PopupParams.DIRECTION_TOP:
82+
if (popupParams.arrowGravity == PopupParams.GRAVITY_RIGHT) {
83+
int offset = arrowViewWidth / 2;
84+
arrowViewLayoutParams.leftMargin = (int) (mParams.dialogParams.absoluteWidth * (1 - ARROW_WEIGHT)) - offset;
85+
}
86+
break;
87+
}
88+
itemsViewView.removeOnLayoutChangeListener(this);
89+
}
7190
arrowView.setLayoutParams(arrowViewLayoutParams);
7291
}
7392
});
74-
mRootCardViewByLinearLayout.addView(itemsViewView);
7593
}
7694
}
7795

0 commit comments

Comments
 (0)