Skip to content

Commit a3744c8

Browse files
committed
fix: 修复一些bug
1 parent 9765c06 commit a3744c8

File tree

6 files changed

+69
-55
lines changed

6 files changed

+69
-55
lines changed

layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java

+35-48
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import androidx.core.graphics.Insets;
1515
import androidx.core.view.ViewCompat;
1616
import androidx.core.view.WindowInsetsCompat;
17-
import androidx.core.view.WindowInsetsControllerCompat;
1817

1918
import per.goweii.layer.core.utils.Utils;
2019

@@ -25,7 +24,6 @@ public class DecorLayer extends FrameLayer {
2524
private final Rect mTempRect = new Rect();
2625

2726
private Runnable mShowRunnable = null;
28-
private WindowInsetsChangedListener mWindowInsetsChangedListener = null;
2927

3028
public DecorLayer(@NonNull Context context) {
3129
this(Utils.requireActivity(context));
@@ -87,28 +85,16 @@ public LayoutInflater getLayoutInflater() {
8785
@Override
8886
protected void onAttach() {
8987
super.onAttach();
90-
Rect decorInsets = getDecorInsets();
91-
fitDecorInsets(decorInsets);
92-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
93-
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView());
94-
if (windowInsetsController != null) {
95-
if (mWindowInsetsChangedListener == null) {
96-
mWindowInsetsChangedListener = new WindowInsetsChangedListener();
97-
}
98-
windowInsetsController.addOnControllableInsetsChangedListener(mWindowInsetsChangedListener);
99-
}
100-
}
88+
getDecorInsets(mInsets);
89+
fitDecorInsets(mInsets);
10190
}
10291

10392
@CallSuper
10493
@Override
10594
protected void onDetach() {
10695
super.onDetach();
107-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
108-
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView());
109-
if (windowInsetsController != null && mWindowInsetsChangedListener != null) {
110-
windowInsetsController.removeOnControllableInsetsChangedListener(mWindowInsetsChangedListener);
111-
}
96+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
97+
ViewCompat.setOnApplyWindowInsetsListener(getViewHolder().getChild(), null);
11298
}
11399
}
114100

@@ -118,39 +104,50 @@ protected void onConfigurationChanged(@NonNull Configuration newConfig) {
118104
Utils.onViewLayout(getViewHolder().getChild(), new Runnable() {
119105
@Override
120106
public void run() {
121-
Rect decorInsets = getDecorInsets();
122-
fitDecorInsets(decorInsets);
107+
if (!mActivity.isDestroyed() && isShown()) {
108+
getDecorInsets(mInsets);
109+
fitDecorInsets(mInsets);
110+
}
123111
}
124112
});
125113
}
126114

127-
protected void fitDecorInsets(@NonNull Rect insets) {
128-
getViewHolder().getParent().setClipToPadding(false);
129-
getViewHolder().getParent().setClipChildren(false);
130-
Utils.setViewPadding(getViewHolder().getParent(), insets);
115+
@Override
116+
protected void onGlobalLayout() {
117+
super.onGlobalLayout();
118+
if (!mActivity.isDestroyed() && isShown()) {
119+
getDecorInsets(mInsets);
120+
fitDecorInsets(mInsets);
121+
}
131122
}
132123

133-
@NonNull
134-
protected final Rect getDecorInsets() {
135-
mInsets.setEmpty();
124+
protected final void getDecorInsets(@NonNull Rect insets) {
125+
insets.setEmpty();
136126
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
137-
WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getActivity().getWindow().getDecorView());
138-
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
139-
mInsets.set(insets.left, insets.top, insets.right, insets.bottom);
127+
WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getViewHolder().getDecor());
128+
if (windowInsets != null) {
129+
Insets realInsets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.ime() | WindowInsetsCompat.Type.displayCutout());
130+
insets.set(realInsets.left, realInsets.top, realInsets.right, realInsets.bottom);
131+
}
140132
} else {
141133
Utils.getViewMargin(getViewHolder().getDecorChild(), mTempRect);
142-
mInsets.set(mTempRect);
134+
insets.set(mTempRect);
143135
Utils.getViewPadding(getViewHolder().getDecorChild(), mTempRect);
144-
mInsets.left += mTempRect.left;
145-
mInsets.top += mTempRect.top;
146-
mInsets.right += mTempRect.right;
147-
mInsets.bottom += mTempRect.bottom;
136+
insets.left += mTempRect.left;
137+
insets.top += mTempRect.top;
138+
insets.right += mTempRect.right;
139+
insets.bottom += mTempRect.bottom;
148140
int statusBarHeightIfVisible = Utils.getStatusBarHeightIfVisible(getActivity());
149-
if (mInsets.top < statusBarHeightIfVisible) {
150-
mInsets.top = statusBarHeightIfVisible;
141+
if (insets.top < statusBarHeightIfVisible) {
142+
insets.top = statusBarHeightIfVisible;
151143
}
152144
}
153-
return mInsets;
145+
}
146+
147+
protected void fitDecorInsets(@NonNull Rect insets) {
148+
getViewHolder().getParent().setClipToPadding(false);
149+
getViewHolder().getParent().setClipChildren(false);
150+
Utils.setViewPadding(getViewHolder().getParent(), insets);
154151
}
155152

156153
public void showImmediately(boolean withAnim) {
@@ -185,16 +182,6 @@ public void dismiss(boolean withAnim) {
185182
}
186183
}
187184

188-
private class WindowInsetsChangedListener implements WindowInsetsControllerCompat.OnControllableInsetsChangedListener {
189-
@Override
190-
public void onControllableInsetsChanged(@NonNull WindowInsetsControllerCompat controller, int typeMask) {
191-
if (isShown()) {
192-
Rect decorInsets = getDecorInsets();
193-
fitDecorInsets(decorInsets);
194-
}
195-
}
196-
}
197-
198185
public static class ViewHolder extends FrameLayer.ViewHolder {
199186
private FrameLayout mActivityContent;
200187
private View mDecorChild;

layer-guide/src/main/java/per/goweii/layer/guide/GuideLayer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ protected void onAttach() {
107107
Utils.onViewLayout(getViewHolder().getChild(), new Runnable() {
108108
@Override
109109
public void run() {
110-
updateLocation();
110+
if (isShown()) {
111+
updateLocation();
112+
}
111113
}
112114
});
113115
}

layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ protected void onAttach() {
8686
Utils.getViewSize(getViewHolder().getContainer(), new Runnable() {
8787
@Override
8888
public void run() {
89-
updateLocation();
89+
if (isShown()) {
90+
updateLocation();
91+
}
9092
}
9193
});
9294
ViewTreeObserver viewTreeObserver = getViewHolder().getParent().getViewTreeObserver();
@@ -100,7 +102,9 @@ public void onScrollChanged() {
100102
if (getConfig().mOnViewTreeScrollChangedListener != null) {
101103
getConfig().mOnViewTreeScrollChangedListener.onScrollChanged();
102104
}
103-
updateLocation();
105+
if (isShown()) {
106+
updateLocation();
107+
}
104108
}
105109
};
106110
viewTreeObserver.addOnScrollChangedListener(mOnScrollChangedListener);
@@ -173,7 +177,9 @@ protected void fitDecorInsets(@NonNull Rect insets) {
173177
Utils.onViewLayout(getViewHolder().getDecor(), new Runnable() {
174178
@Override
175179
public void run() {
176-
updateLocation();
180+
if (isShown()) {
181+
updateLocation();
182+
}
177183
}
178184
});
179185
}
@@ -390,7 +396,9 @@ private void initContentLocation(int targetX, int targetY, int targetWidth, int
390396
Utils.onViewLayout(getViewHolder().getContentWrapper(), new Runnable() {
391397
@Override
392398
public void run() {
393-
updateLocation();
399+
if (isShown()) {
400+
updateLocation();
401+
}
394402
}
395403
});
396404
}
@@ -478,7 +486,9 @@ private void initBackgroundLocation() {
478486
Utils.onViewLayout(getViewHolder().getBackground(), new Runnable() {
479487
@Override
480488
public void run() {
481-
updateLocation();
489+
if (isShown()) {
490+
updateLocation();
491+
}
482492
}
483493
});
484494
}

simple/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</activity>
2121
<activity
2222
android:name=".LayersSimpleActivity"
23+
android:windowSoftInputMode="adjustResize|stateAlwaysVisible"
2324
android:configChanges="orientation|screenSize|smallestScreenSize|density|keyboardHidden|keyboard" />
2425
<activity
2526
android:name=".CupertinoSimpleActivity"

simple/src/main/java/per/goweii/layer/simple/LayersSimpleActivity.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package per.goweii.layer.simple
22

33
import android.content.Context
4+
import android.content.Intent
5+
import android.net.Uri
46
import android.os.Bundle
7+
import android.provider.Settings
58
import android.view.Gravity
69
import android.view.View
710
import android.view.inputmethod.InputMethodManager
811
import android.widget.EditText
912
import androidx.appcompat.app.AppCompatActivity
1013
import per.goweii.layer.core.anim.AnimStyle
1114
import per.goweii.layer.core.anim.NullAnimatorCreator
15+
import per.goweii.layer.core.ktx.onClick
1216
import per.goweii.layer.core.ktx.onClickToDismiss
1317
import per.goweii.layer.core.ktx.onPreDismiss
1418
import per.goweii.layer.core.ktx.onPreShow
@@ -24,6 +28,7 @@ import per.goweii.layer.overlay.ktx.*
2428
import per.goweii.layer.popup.PopupLayer
2529
import per.goweii.layer.toast.ToastLayer
2630

31+
2732
class LayersSimpleActivity : AppCompatActivity() {
2833
override fun onCreate(savedInstanceState: Bundle?) {
2934
super.onCreate(savedInstanceState)
@@ -48,7 +53,12 @@ class LayersSimpleActivity : AppCompatActivity() {
4853
.contentView(R.layout.dialog_normal)
4954
.backgroundDimDefault()
5055
.onClickToDismiss(R.id.fl_dialog_no)
51-
.onClickToDismiss(R.id.fl_dialog_yes)
56+
.onClick(R.id.fl_dialog_yes) {
57+
startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
58+
data = Uri.fromParts("package", packageName, null)
59+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
60+
})
61+
}
5262
.show()
5363
}
5464

simple/src/main/res/layout/activity_layers_simple.xml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
android:gravity="center"
1111
android:orientation="vertical">
1212

13+
<androidx.appcompat.widget.AppCompatEditText
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content" />
16+
1317
<androidx.appcompat.widget.AppCompatButton
1418
android:id="@+id/btn_dialog"
1519
style="@style/ButtonStyleListItem"

0 commit comments

Comments
 (0)