1、首先是右→左进入动画 、slide_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="800"android:fromXDelta="200%"android:toXDelta="0" />
</set>
左→右退出动画、slide_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="800"android:fromXDelta="0"android:toXDelta="200%" />
</set>
2、activity 的xml代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/mainks"android:layout_width="200dp"android:layout_height="100dp"android:gravity="center"android:text="开始"/><TextViewandroid:id="@+id/maingb"android:layout_width="200dp"android:layout_height="100dp"android:gravity="center"android:text="关闭"/></LinearLayout><android.support.design.widget.CoordinatorLayoutandroid:id="@+id/clRootView"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.design.widget.CoordinatorLayout></FrameLayout>
3、activity实现方法
boolean ifshowfragment = false;
抽屉fragment进入方法yhox
private void showAboutFragment() {getSupportFragmentManager().beginTransaction().disallowAddToBackStack().setCustomAnimations(R.anim.slide_left, R.anim.slide_right).add(R.id.clRootView, MyFragment.newInstance(), MyFragment.TAG).commit();ifshowfragment = true;
}
抽屉fragment退出方法
public void onFragmentDetached(String tag) {FragmentManager fragmentManager = getSupportFragmentManager();Fragment fragment = fragmentManager.findFragmentByTag(tag);if (fragment != null) {fragmentManager.beginTransaction().disallowAddToBackStack().setCustomAnimations(R.anim.slide_left, R.anim.slide_right).remove(fragment).commitNow();}ifshowfragment = false;
}
activity返回键退出动画方法:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK&& event.getAction() == KeyEvent.ACTION_DOWN) {if (ifshowfragment) {onFragmentDetached(MyFragment.TAG);return true;}}return super.onKeyDown(keyCode, event);
}
demo云盘链接:https://pan.baidu.com/s/1t__nXXnfyEUVA24gPOKRAQ
在线回复密码QQ:1085220040
半屏导航fragment实现方法
1、添加导航view依赖
compile 'com.android.support:design:28.0.0'
2、activity代码:
private NavigationView navigationView;
private DrawerLayout drawerLayout;
navigationView = (NavigationView)findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
drawerLayout.openDrawer(navigationView);
drawerLayout.closeDrawer(navigationView);
效果:
全屏 半屏