天天看點

動态顯示和隐藏NavigationBar

修改地方在NavigationBarFragment.java類中

public static View create(Context context, FragmentListener listener) {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
                WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
                        | WindowManager.LayoutParams.FLAG_SLIPPERY,
                PixelFormat.TRANSLUCENT);
        lp.token = new Binder();
        lp.setTitle("NavigationBar");
        lp.windowAnimations = 0;

        View navigationBarView = LayoutInflater.from(context).inflate(
                R.layout.navigation_bar_window, null);


        navigationBarView.setVisibility(View.GONE);

        if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
        if (navigationBarView == null) return null;


        FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
        NavigationBarFragment fragment = new NavigationBarFragment();

         //替換成新的Fragment
        context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
        replaceFragment(fragmentHost,fragment,TAG);
        fragmentHost.addTagListener(TAG, listener);

          //移除指定的Fragment
        context.getSystemService(WindowManager.class).removeView(navigationBarView);
        removeFragment(fragmentHost,fragment);
        fragmentHost.removeTagListener(TAG, listener);

        return navigationBarView;
    }

    //移除指定的Fragment
    private static void removeFragment(FragmentHostManager fragmentHost,Fragment fragment){
        fragmentHost.getFragmentManager().beginTransaction()
                .remove(fragment)
                .commit();
    }

    // 清空fragmentList的所有Fragment,替換成新的Fragment,注意Fragment裡面的坑
    private static void replaceFragment(FragmentHostManager fragmentHost,Fragment fragment, String TAG){
        fragmentHost.getFragmentManager().beginTransaction()
                .replace(R.id.navigation_bar_frame, fragment, TAG)
                .commit();
    }
           

繼續閱讀