Skip to content

Commit 617f6b6

Browse files
committed
修复弹窗共用decorView在某种情况下无法remove的问题
1 parent 9642a1e commit 617f6b6

File tree

10 files changed

+106
-45
lines changed

10 files changed

+106
-45
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<category android:name="android.intent.category.LAUNCHER" />
2323
</intent-filter>
2424
</activity>
25+
<activity android:name=".DemoActivity" />
2526
</application>
2627

2728
</manifest>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.lxj.xpopupdemo;
2+
3+
import android.os.Bundle;
4+
import android.os.Handler;
5+
import android.support.annotation.Nullable;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.widget.Toast;
8+
9+
import com.lxj.xpopup.XPopup;
10+
import com.lxj.xpopup.interfaces.OnSelectListener;
11+
12+
/**
13+
* Description:
14+
* Create by lxj, at 2019/2/2
15+
*/
16+
public class DemoActivity extends AppCompatActivity {
17+
@Override
18+
protected void onCreate(@Nullable Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
21+
XPopup.get(this).asBottomList("haha", new String[]{"aaaa", "bbbb"}, new OnSelectListener() {
22+
@Override
23+
public void onSelect(int position, String text) {
24+
Toast.makeText(DemoActivity.this, text, 0).show();
25+
}
26+
}).show();
27+
28+
new Handler().postDelayed(new Runnable() {
29+
@Override
30+
public void run() {
31+
32+
XPopup.get(DemoActivity.this).asLoading().show();
33+
}
34+
},0);
35+
}
36+
}

app/src/main/java/com/lxj/xpopupdemo/XPopupApp.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class XPopupApp extends Application {
1111
@Override
1212
public void onCreate() {
1313
super.onCreate();
14-
// if (LeakCanary.isInAnalyzerProcess(this)) {
15-
// return;
16-
// }
17-
// LeakCanary.install(this);
14+
if (LeakCanary.isInAnalyzerProcess(this)) {
15+
return;
16+
}
17+
LeakCanary.install(this);
1818
}
1919
}

app/src/main/java/com/lxj/xpopupdemo/custompopup/ZhihuCommentPopup.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lxj.xpopupdemo.custompopup;
22

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.support.annotation.NonNull;
56
import android.support.v7.widget.RecyclerView;
67
import android.view.View;
@@ -11,6 +12,7 @@
1112
import com.lxj.xpopup.core.BottomPopupView;
1213
import com.lxj.xpopup.util.XPopupUtils;
1314
import com.lxj.xpopup.widget.VerticalRecyclerView;
15+
import com.lxj.xpopupdemo.DemoActivity;
1416
import com.lxj.xpopupdemo.R;
1517

1618
import java.util.ArrayList;
@@ -45,12 +47,8 @@ protected void convert(@NonNull ViewHolder holder, @NonNull String s, int positi
4547
commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener(){
4648
@Override
4749
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
48-
postDelayed(new Runnable() {
49-
@Override
50-
public void run() {
51-
dismiss();
52-
}
53-
},200);
50+
dismiss();
51+
getContext().startActivity(new Intent(getContext(), DemoActivity.class));
5452
}
5553
});
5654
recyclerView.setHasFixedSize(true);

app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,15 @@ public void onSelect(int position, String text) {
186186
}
187187
}
188188

189+
@Override
190+
public void onPause() {
191+
super.onPause();
192+
Log.e("tag", "onPause");
193+
}
189194

190-
191-
195+
@Override
196+
public void onStop() {
197+
super.onStop();
198+
Log.e("tag", "onStop");
199+
}
192200
}

library/src/main/java/com/lxj/xpopup/XPopup.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Color;
66
import android.graphics.PointF;
77
import android.os.Handler;
8+
import android.util.Log;
89
import android.view.MotionEvent;
910
import android.view.View;
1011
import android.view.ViewGroup;
@@ -46,8 +47,6 @@ public class XPopup {
4647
private static WeakReference<Context> contextRef;
4748
private PopupInfo tempInfo = null;
4849
private BasePopupView tempView;
49-
private Handler handler = new Handler();
50-
private static ViewGroup decorView = null;
5150
private int primaryColor = Color.parseColor("#121212");
5251
private static ArrayList<BasePopupView> popupViews = new ArrayList<>();
5352

@@ -121,39 +120,51 @@ private void showInternal(final BasePopupView pv) {
121120
}
122121
if (pv.getParent() != null) return;
123122
final Activity activity = (Activity) contextRef.get();
124-
decorView = (ViewGroup) activity.getWindow().getDecorView();
125-
decorView.addView(pv, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
126-
FrameLayout.LayoutParams.MATCH_PARENT));
127-
decorView.bringChildToFront(pv);
128-
129-
//2. 执行初始化
130-
pv.init(new Runnable() { // 弹窗显示动画执行完毕调用
123+
pv.popupInfo.decorView = (ViewGroup) activity.getWindow().getDecorView();
124+
// add PopupView to its decorView after measured.
125+
pv.popupInfo.decorView.post(new Runnable() {
131126
@Override
132127
public void run() {
133-
if (pv.popupInfo != null && pv.popupInfo.xPopupCallback != null)
134-
pv.popupInfo.xPopupCallback.onShow();
135-
136-
if (XPopupUtils.getDecorViewInvisibleHeight(activity) > 0) {
137-
XPopupUtils.moveUpToKeyboard(XPopupUtils.getDecorViewInvisibleHeight(activity), pv);
128+
if(pv.getParent()!=null){
129+
((ViewGroup)pv.getParent()).removeView(pv);
138130
}
131+
pv.popupInfo.decorView.addView(pv, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
132+
FrameLayout.LayoutParams.MATCH_PARENT));
139133

140-
}
141-
}, new Runnable() { // 弹窗消失动画执行完毕调用
142-
@Override
143-
public void run() {
144-
popupViews.remove(pv);
145-
if (pv.popupInfo != null && pv.popupInfo.xPopupCallback != null)
146-
pv.popupInfo.xPopupCallback.onDismiss();
134+
//2. 执行初始化
135+
pv.init(new Runnable() { // 弹窗显示动画执行完毕调用
136+
@Override
137+
public void run() {
138+
if (pv.popupInfo != null && pv.popupInfo.xPopupCallback != null)
139+
pv.popupInfo.xPopupCallback.onShow();
147140

148-
// 移除弹窗
149-
if (decorView != null) decorView.removeView(pv);
141+
if (XPopupUtils.getDecorViewInvisibleHeight(activity) > 0) {
142+
XPopupUtils.moveUpToKeyboard(XPopupUtils.getDecorViewInvisibleHeight(activity), pv);
143+
}
150144

151-
// 释放对象
152-
release();
145+
}
146+
}, new Runnable() { // 弹窗消失动画执行完毕调用
147+
@Override
148+
public void run() {
149+
// 移除弹窗
150+
pv.popupInfo.decorView.removeView(pv);
151+
KeyboardUtils.removeLayoutChangeListener(pv.popupInfo.decorView);
152+
popupViews.remove(pv);
153+
if (pv.popupInfo != null && pv.popupInfo.xPopupCallback != null) {
154+
pv.popupInfo.xPopupCallback.onDismiss();
155+
}
156+
pv.popupInfo = null;
157+
158+
// 释放对象
159+
release();
160+
}
161+
});
153162
}
154163
});
155164
}
156165

166+
167+
157168
/**
158169
* 消失
159170
*/
@@ -189,11 +200,8 @@ public void dismiss(Object tag) {
189200
*/
190201
private void release() {
191202
if (!popupViews.isEmpty()) return;
192-
handler.removeCallbacks(null);
193203
if (contextRef != null) contextRef.clear();
194-
if (decorView != null) KeyboardUtils.removeLayoutChangeListener(decorView);
195204
contextRef = null;
196-
decorView = null;
197205
}
198206

199207
/**

library/src/main/java/com/lxj/xpopup/core/BasePopupView.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public BasePopupView(@NonNull Context context) {
5151
// 事先隐藏,等测量完毕恢复。避免View影子跳动现象
5252
contentView.setAlpha(0);
5353
addView(contentView);
54-
// 如果有导航栏,则不能覆盖导航栏,
55-
if (XPopupUtils.isNavBarVisible(getContext())) {
56-
setPadding(0, 0, 0, XPopupUtils.getNavBarHeight());
57-
}
5854
}
5955

6056
public BasePopupView(@NonNull Context context, @Nullable AttributeSet attrs) {
@@ -119,6 +115,10 @@ public void init(final Runnable afterAnimationStarted, Runnable afterAnimationEn
119115
post(new Runnable() {
120116
@Override
121117
public void run() {
118+
// 如果有导航栏,则不能覆盖导航栏,
119+
if (XPopupUtils.isNavBarVisible(getContext())) {
120+
setPadding(0, 0, 0, XPopupUtils.getNavBarHeight());
121+
}
122122
getPopupContentView().setAlpha(1f);
123123

124124
//2. 收集动画执行器
@@ -301,7 +301,6 @@ protected void doAfterDismiss(){
301301
public void run() {
302302
afterAnimationEnd.run();
303303
popupStatus = PopupStatus.Dismiss;
304-
popupInfo = null;
305304
}
306305
}, getAnimationDuration());
307306
}

library/src/main/java/com/lxj/xpopup/core/BottomPopupView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.support.annotation.NonNull;
5+
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
78

library/src/main/java/com/lxj/xpopup/core/PopupInfo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.graphics.PointF;
44
import android.view.View;
5+
import android.view.ViewGroup;
56

67
import com.lxj.xpopup.animator.PopupAnimator;
78
import com.lxj.xpopup.enums.PopupAnimation;
@@ -27,6 +28,8 @@ public class PopupInfo {
2728

2829
public XPopupCallback xPopupCallback;
2930

31+
public ViewGroup decorView; //每个弹窗所属的DecorView
32+
3033
public View getAtView() {
3134
return atView;
3235
}

library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.support.v4.view.ViewCompat;
88
import android.support.v7.widget.CardView;
99
import android.util.AttributeSet;
10+
import android.util.Log;
1011
import android.view.MotionEvent;
1112
import android.view.View;
1213
import android.view.ViewConfiguration;
@@ -134,24 +135,29 @@ public void scrollTo(int x, int y) {
134135
if (isUserClose && fraction == 0f && listener != null) {
135136
listener.onClose();
136137
}
138+
Log.e("tag", "scrollTo fraction: "+fraction);
137139
isScrollUp = y > getScrollY();
138140
super.scrollTo(x, y);
139141
}
140142

141143
@Override
142144
public void computeScroll() {
143145
super.computeScroll();
146+
// Log.e("tag", "scroller.isFinished() : "+scroller.isFinished() +
147+
// " scroller.computeScrollOffset(): "+scroller.computeScrollOffset());
144148
if (scroller.computeScrollOffset()) {
145149
scrollTo(scroller.getCurrX(), scroller.getCurrY());
146150
ViewCompat.postInvalidateOnAnimation(this);
147151
}
148152
}
149153

154+
150155
@Override
151156
protected void onDetachedFromWindow() {
152157
super.onDetachedFromWindow();
153158
isScrollUp = false;
154159
isUserClose = false;
160+
Log.e("tag", "onDetachedFromWindow isUserClose: "+isUserClose);
155161
}
156162

157163
public void open() {
@@ -160,6 +166,7 @@ public void open() {
160166
}
161167

162168
public void close() {
169+
Log.e("tag", "close isUserClose: "+isUserClose);
163170
isUserClose = true;
164171
scroller.startScroll(getScrollX(), getScrollY(), 0, minY - getScrollY(), 500);
165172
ViewCompat.postInvalidateOnAnimation(this);

0 commit comments

Comments
 (0)