Skip to content

Commit

Permalink
fix: 修复一些bug
Browse files Browse the repository at this point in the history
  • Loading branch information
goweii committed Apr 17, 2022
1 parent 9765c06 commit a3744c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 55 deletions.
83 changes: 35 additions & 48 deletions layer-core/src/main/java/per/goweii/layer/core/DecorLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;

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

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

private Runnable mShowRunnable = null;
private WindowInsetsChangedListener mWindowInsetsChangedListener = null;

public DecorLayer(@NonNull Context context) {
this(Utils.requireActivity(context));
Expand Down Expand Up @@ -87,28 +85,16 @@ public LayoutInflater getLayoutInflater() {
@Override
protected void onAttach() {
super.onAttach();
Rect decorInsets = getDecorInsets();
fitDecorInsets(decorInsets);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView());
if (windowInsetsController != null) {
if (mWindowInsetsChangedListener == null) {
mWindowInsetsChangedListener = new WindowInsetsChangedListener();
}
windowInsetsController.addOnControllableInsetsChangedListener(mWindowInsetsChangedListener);
}
}
getDecorInsets(mInsets);
fitDecorInsets(mInsets);
}

@CallSuper
@Override
protected void onDetach() {
super.onDetach();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView());
if (windowInsetsController != null && mWindowInsetsChangedListener != null) {
windowInsetsController.removeOnControllableInsetsChangedListener(mWindowInsetsChangedListener);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewCompat.setOnApplyWindowInsetsListener(getViewHolder().getChild(), null);
}
}

Expand All @@ -118,39 +104,50 @@ protected void onConfigurationChanged(@NonNull Configuration newConfig) {
Utils.onViewLayout(getViewHolder().getChild(), new Runnable() {
@Override
public void run() {
Rect decorInsets = getDecorInsets();
fitDecorInsets(decorInsets);
if (!mActivity.isDestroyed() && isShown()) {
getDecorInsets(mInsets);
fitDecorInsets(mInsets);
}
}
});
}

protected void fitDecorInsets(@NonNull Rect insets) {
getViewHolder().getParent().setClipToPadding(false);
getViewHolder().getParent().setClipChildren(false);
Utils.setViewPadding(getViewHolder().getParent(), insets);
@Override
protected void onGlobalLayout() {
super.onGlobalLayout();
if (!mActivity.isDestroyed() && isShown()) {
getDecorInsets(mInsets);
fitDecorInsets(mInsets);
}
}

@NonNull
protected final Rect getDecorInsets() {
mInsets.setEmpty();
protected final void getDecorInsets(@NonNull Rect insets) {
insets.setEmpty();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getActivity().getWindow().getDecorView());
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
mInsets.set(insets.left, insets.top, insets.right, insets.bottom);
WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(getViewHolder().getDecor());
if (windowInsets != null) {
Insets realInsets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.ime() | WindowInsetsCompat.Type.displayCutout());
insets.set(realInsets.left, realInsets.top, realInsets.right, realInsets.bottom);
}
} else {
Utils.getViewMargin(getViewHolder().getDecorChild(), mTempRect);
mInsets.set(mTempRect);
insets.set(mTempRect);
Utils.getViewPadding(getViewHolder().getDecorChild(), mTempRect);
mInsets.left += mTempRect.left;
mInsets.top += mTempRect.top;
mInsets.right += mTempRect.right;
mInsets.bottom += mTempRect.bottom;
insets.left += mTempRect.left;
insets.top += mTempRect.top;
insets.right += mTempRect.right;
insets.bottom += mTempRect.bottom;
int statusBarHeightIfVisible = Utils.getStatusBarHeightIfVisible(getActivity());
if (mInsets.top < statusBarHeightIfVisible) {
mInsets.top = statusBarHeightIfVisible;
if (insets.top < statusBarHeightIfVisible) {
insets.top = statusBarHeightIfVisible;
}
}
return mInsets;
}

protected void fitDecorInsets(@NonNull Rect insets) {
getViewHolder().getParent().setClipToPadding(false);
getViewHolder().getParent().setClipChildren(false);
Utils.setViewPadding(getViewHolder().getParent(), insets);
}

public void showImmediately(boolean withAnim) {
Expand Down Expand Up @@ -185,16 +182,6 @@ public void dismiss(boolean withAnim) {
}
}

private class WindowInsetsChangedListener implements WindowInsetsControllerCompat.OnControllableInsetsChangedListener {
@Override
public void onControllableInsetsChanged(@NonNull WindowInsetsControllerCompat controller, int typeMask) {
if (isShown()) {
Rect decorInsets = getDecorInsets();
fitDecorInsets(decorInsets);
}
}
}

public static class ViewHolder extends FrameLayer.ViewHolder {
private FrameLayout mActivityContent;
private View mDecorChild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ protected void onAttach() {
Utils.onViewLayout(getViewHolder().getChild(), new Runnable() {
@Override
public void run() {
updateLocation();
if (isShown()) {
updateLocation();
}
}
});
}
Expand Down
20 changes: 15 additions & 5 deletions layer-popup/src/main/java/per/goweii/layer/popup/PopupLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ protected void onAttach() {
Utils.getViewSize(getViewHolder().getContainer(), new Runnable() {
@Override
public void run() {
updateLocation();
if (isShown()) {
updateLocation();
}
}
});
ViewTreeObserver viewTreeObserver = getViewHolder().getParent().getViewTreeObserver();
Expand All @@ -100,7 +102,9 @@ public void onScrollChanged() {
if (getConfig().mOnViewTreeScrollChangedListener != null) {
getConfig().mOnViewTreeScrollChangedListener.onScrollChanged();
}
updateLocation();
if (isShown()) {
updateLocation();
}
}
};
viewTreeObserver.addOnScrollChangedListener(mOnScrollChangedListener);
Expand Down Expand Up @@ -173,7 +177,9 @@ protected void fitDecorInsets(@NonNull Rect insets) {
Utils.onViewLayout(getViewHolder().getDecor(), new Runnable() {
@Override
public void run() {
updateLocation();
if (isShown()) {
updateLocation();
}
}
});
}
Expand Down Expand Up @@ -390,7 +396,9 @@ private void initContentLocation(int targetX, int targetY, int targetWidth, int
Utils.onViewLayout(getViewHolder().getContentWrapper(), new Runnable() {
@Override
public void run() {
updateLocation();
if (isShown()) {
updateLocation();
}
}
});
}
Expand Down Expand Up @@ -478,7 +486,9 @@ private void initBackgroundLocation() {
Utils.onViewLayout(getViewHolder().getBackground(), new Runnable() {
@Override
public void run() {
updateLocation();
if (isShown()) {
updateLocation();
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions simple/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</activity>
<activity
android:name=".LayersSimpleActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible"
android:configChanges="orientation|screenSize|smallestScreenSize|density|keyboardHidden|keyboard" />
<activity
android:name=".CupertinoSimpleActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package per.goweii.layer.simple

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.Gravity
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import per.goweii.layer.core.anim.AnimStyle
import per.goweii.layer.core.anim.NullAnimatorCreator
import per.goweii.layer.core.ktx.onClick
import per.goweii.layer.core.ktx.onClickToDismiss
import per.goweii.layer.core.ktx.onPreDismiss
import per.goweii.layer.core.ktx.onPreShow
Expand All @@ -24,6 +28,7 @@ import per.goweii.layer.overlay.ktx.*
import per.goweii.layer.popup.PopupLayer
import per.goweii.layer.toast.ToastLayer


class LayersSimpleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -48,7 +53,12 @@ class LayersSimpleActivity : AppCompatActivity() {
.contentView(R.layout.dialog_normal)
.backgroundDimDefault()
.onClickToDismiss(R.id.fl_dialog_no)
.onClickToDismiss(R.id.fl_dialog_yes)
.onClick(R.id.fl_dialog_yes) {
startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", packageName, null)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
})
}
.show()
}

Expand Down
4 changes: 4 additions & 0 deletions simple/src/main/res/layout/activity_layers_simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
android:gravity="center"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_dialog"
style="@style/ButtonStyleListItem"
Expand Down

0 comments on commit a3744c8

Please sign in to comment.