From aa3a4681b7f9133776f2d0cceae075f92a1fdbcb Mon Sep 17 00:00:00 2001 From: "pengjian@szy.cn" Date: Tue, 28 Mar 2017 10:48:39 +0800 Subject: [PATCH 1/3] Fix root background not apply --- .../swipebacklayout/lib/SwipeBackLayout.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java index 2f56936..9a03cf7 100644 --- a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java +++ b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java @@ -4,15 +4,20 @@ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Rect; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.os.Build; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -460,13 +465,42 @@ public void attachToActivity(Activity activity) { ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView(); ViewGroup decorChild = (ViewGroup) decor.getChildAt(0); - decorChild.setBackgroundResource(background); + Drawable d = decorChild.getBackground(); + // no background or background is TRANSPARENT,use 'windowBackground' to background + if (d == null || (d instanceof ColorDrawable && isTransparent((ColorDrawable) d))) { + decorChild.setBackgroundResource(background); + } decor.removeView(decorChild); addView(decorChild); setContentView(decorChild); decor.addView(this); } + private boolean isTransparent(ColorDrawable drawable) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + return drawable.getColor() == Color.TRANSPARENT; + } else { + try { + int color = 0; + final Field stateField = drawable.getClass().getDeclaredField("mState"); + stateField.setAccessible(true); + final Object state = stateField.get(drawable); + + final Field useColorField = state.getClass().getDeclaredField("mUseColor"); + useColorField.setAccessible(true); + useColorField.setInt(state, color); + + final Field baseColorField = state.getClass().getDeclaredField( + "mBaseColor"); + baseColorField.setAccessible(true); + baseColorField.setInt(state, color); + return color == Color.TRANSPARENT; + } catch (Exception e) { + return false; + } + } + } + @Override public void computeScroll() { mScrimOpacity = 1 - mScrollPercent; @@ -549,7 +583,7 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i if (mScrollPercent >= 1) { if (!mActivity.isFinishing()) { mActivity.finish(); - mActivity.overridePendingTransition(0, 0); + mActivity.overridePendingTransition(0, 0); } } } From 282bded12479d3ae31c15908c958e4d3f1793653 Mon Sep 17 00:00:00 2001 From: "pengjian@szy.cn" Date: Fri, 7 Apr 2017 15:50:24 +0800 Subject: [PATCH 2/3] Fix attachToActivity bug --- .../main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java index 9a03cf7..f7a3f0d 100644 --- a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java +++ b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java @@ -473,7 +473,7 @@ public void attachToActivity(Activity activity) { decor.removeView(decorChild); addView(decorChild); setContentView(decorChild); - decor.addView(this); + decor.addView(this,0); } private boolean isTransparent(ColorDrawable drawable) { From d4a7c5b80b4c5dc08932e1a9108d7563b96af5eb Mon Sep 17 00:00:00 2001 From: "pengjian@szy.cn" Date: Fri, 7 Apr 2017 16:05:02 +0800 Subject: [PATCH 3/3] Fix attachToActivity bug --- .../java/me/imid/swipebacklayout/lib/SwipeBackLayout.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java index f7a3f0d..3a25f83 100644 --- a/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java +++ b/library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java @@ -473,6 +473,11 @@ public void attachToActivity(Activity activity) { decor.removeView(decorChild); addView(decorChild); setContentView(decorChild); + + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) decorChild.getLayoutParams(); + decorChild.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + setLayoutParams(lp); + decor.addView(this,0); }