Skip to content

Commit

Permalink
修复内存泄露和PartShadow弹窗被导航栏覆盖的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Jan 1, 2019
1 parent 3badc1f commit 39c3d75
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ dependencies {
implementation 'com.lxj:easyadapter:0.0.8'
implementation project(':library')

debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.2'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".XPopupApp"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lxj/xpopupdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.lxj.xpopupdemo.fragment.CustomPopupDemo;
import com.lxj.xpopupdemo.fragment.PartShadowDemo;
import com.lxj.xpopupdemo.fragment.QuickStartDemo;
import com.squareup.leakcanary.LeakCanary;

public class MainActivity extends AppCompatActivity {

Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/lxj/xpopupdemo/XPopupApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lxj.xpopupdemo;

import android.app.Application;

import com.squareup.leakcanary.LeakCanary;

/**
* Description:
* Create by dance, at 2019/1/1
*/
public class XPopupApp extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
return;
}
LeakCanary.install(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* Create by dance, at 2018/12/21
*/
public class CustomPartShadowPopupView extends PartShadowPopupView {
View container;
public CustomPartShadowPopupView(@NonNull Context context) {
super(context);
}
Expand All @@ -24,7 +23,6 @@ protected int getImplLayoutId() {
@Override
protected void initPopupContent() {
super.initPopupContent();
container = findViewById(R.id.container);
findViewById(R.id.btnClose).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/custom_part_shadow_popup.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/container"
android:clickable="true"
android:background="#f1f1f1"
android:layout_height="wrap_content">
<TextView
Expand Down
18 changes: 11 additions & 7 deletions library/src/main/java/com/lxj/xpopup/XPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.lxj.xpopup.animator.PopupAnimator;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class XPopup implements BasePopupView.DismissProxy {
private static WeakReference<Context> contextRef;
private PopupInfo popupInfo = null;
private Handler handler = new Handler();
private ViewGroup activityView = null;
private ViewGroup decorView = null;
private PopupStatus popupStatus = PopupStatus.Dismiss;
private BasePopupView popupView;
private XPopupCallback xPopupCallback;
Expand Down Expand Up @@ -72,15 +73,15 @@ public void show() {
}
Activity activity = (Activity) contextRef.get();

activityView = (ViewGroup) activity.getWindow().getDecorView();
decorView = (ViewGroup) activity.getWindow().getDecorView();

//1. set popupView
popupView.setPopupInfo(popupInfo);
popupView.setDismissProxy(this);

activityView.addView(popupView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
decorView.addView(popupView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
activityView.bringChildToFront(popupView);
decorView.bringChildToFront(popupView);
popupStatus = PopupStatus.Showing;

//2. 执行初始化
Expand Down Expand Up @@ -125,10 +126,13 @@ public void run() {
contextRef = null;
if (xPopupCallback != null)
xPopupCallback.onDismiss();
if (activityView != null) {
activityView.removeView(popupView);
activityView = null;
if (decorView != null) {
KeyboardUtils.removeLayoutChangeListener(decorView);
decorView.removeView(popupView);
decorView = null;
}
popupView = null;
xPopupCallback = null;
}
}, popupView.getAnimationDuration());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.lxj.xpopup.animator.TranslateAnimator;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.util.XPopupUtils;

/**
* Description: 局部阴影的弹窗,类似于淘宝商品列表的下拉筛选弹窗
Expand Down Expand Up @@ -58,6 +59,10 @@ protected void doAttach() {
} else {
// atView在上半部分,PartShadow应该显示在它下方,计算atView之下的高度
params.height = getMeasuredHeight() - rect.bottom;
// 防止伸到导航栏下面\
if(XPopupUtils.hasNavigationBar(getContext()) && !XPopupUtils.isFuckDevice()){
params.height -= XPopupUtils.getNavBarHeight();
}
isShowUp = false;
getPopupContentView().setTranslationY(rect.bottom + defaultOffsetY);

Expand Down
10 changes: 8 additions & 2 deletions library/src/main/java/com/lxj/xpopup/util/KeyboardUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
Expand Down Expand Up @@ -43,8 +44,7 @@ private static int getDecorViewInvisibleHeight(final Activity activity) {
* @param activity The activity.
* @param listener The soft input changed listener.
*/
public static void registerSoftInputChangedListener(final Activity activity,
final OnSoftInputChangedListener listener) {
public static void registerSoftInputChangedListener(final Activity activity, final OnSoftInputChangedListener listener) {
final int flags = activity.getWindow().getAttributes().flags;
if ((flags & WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) != 0) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Expand All @@ -68,6 +68,12 @@ public void onGlobalLayout() {
.addOnGlobalLayoutListener(onGlobalLayoutListener);
}

public static void removeLayoutChangeListener(View decorView){
View contentView = decorView.findViewById(android.R.id.content);
contentView.getViewTreeObserver().removeGlobalOnLayoutListener(onGlobalLayoutListener);
onGlobalLayoutListener = null;
}

private static int getNavBarHeight() {
Resources res = Resources.getSystem();
int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
Expand Down

0 comments on commit 39c3d75

Please sign in to comment.