记一次实现左滑关闭页面 返回上个页面,虽然实现的不完美 但这也是一种实现方式 记录学习的过程
实现方式是v4 包下的 SlidingPaneLayout
private void SlideBackClose() {
SlidingPaneLayout slidingPaneLayout = new SlidingPaneLayout(this);
try {
Field overhangSize = SlidingPaneLayout.class.getDeclaredField("mOverhangSize");
overhangSize.setAccessible(true);
overhangSize.set(slidingPaneLayout, 0);
} catch (Exception e) {
e.printStackTrace();
}
slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
@Override
public void onPanelSlide(View panel, float slideOffset) {
}
@Override
public void onPanelOpened(View panel) {
finish();
}
@Override
public void onPanelClosed(View panel) {
}
});
slidingPaneLayout.setSliderFadeColor(getResources()
.getColor(android.R.color.transparent));
View leftView = new View(this);
leftView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
slidingPaneLayout.addView(leftView, 0);
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0);
decorChild.setBackgroundColor(getResources()
.getColor(android.R.color.white));
decorView.removeView(decorChild);
decorView.addView(slidingPaneLayout);
slidingPaneLayout.addView(decorChild, 1);
}
然后设置Activity的主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
<style name="SlidingBack.Theme" parent="AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@style/SlidingBack</item>
</style>
<style name="SlidingBack" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/sliding_in_right</item>
<item name="android:activityOpenExitAnimation">@anim/sliding_out_right</item>
<item name="android:activityCloseEnterAnimation">@anim/sliding_in_right</item>
<item name="android:activityCloseExitAnimation">@anim/sliding_out_right</item>
<item name="android:wallpaperOpenEnterAnimation">@anim/sliding_in_right</item>
<item name="android:wallpaperOpenExitAnimation">@anim/sliding_out_right</item>
<item name="android:wallpaperCloseEnterAnimation">@anim/sliding_in_right</item>
<item name="android:wallpaperCloseExitAnimation">@anim/sliding_out_right</item>
<item name="android:wallpaperIntraOpenEnterAnimation">@anim/sliding_in_right</item>
<item name="android:wallpaperIntraOpenExitAnimation">@anim/sliding_out_right</item>
<item name="android:wallpaperIntraCloseEnterAnimation">@anim/sliding_in_right</item>
<item name="android:wallpaperIntraCloseExitAnimation">@anim/sliding_out_right</item>
</style>
sliding_in_right
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<translate
android:duration="200"
android:fromXDelta="100.0%p"
android:toXDelta="0.0" />
</set>
sliding_out_right
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<translate
android:duration="200"
android:fromXDelta="0.0"
android:toXDelta="100.0%p" />
</set>
网友评论