Android 全屏抽屉fragment,NavigationView

1、首先是右→左进入动画 、slide_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android: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">
    <translate
        android: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">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <TextView
            android:id="@+id/mainks"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:gravity="center"
            android:text="开始"/>

        <TextView
            android:id="@+id/maingb"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:gravity="center"
            android:text="关闭"/>

   
    </LinearLayout>



    <android.support.design.widget.CoordinatorLayout
        android: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);

效果:

全屏                                                                                               半屏

                         

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值