导航抽屉在关闭时抽搐

时间:2015-04-27 13:07:35

标签: java android android-fragments navigation-drawer

我尝试在我的应用中实现导航抽屉(Material Design)。我的活动包含带有片段的FrameLayout。当用户选择导航抽屉FrameLayout中的项目重新加载新片段时:

    listViewDrawer.setOnItemClickListener( new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick( AdapterView<?> parent, View view, int position, long id) {

            drawerLayout.closeDrawer( listViewDrawer); // <<<-------

            toolbar.setTitle( getResources().getString( R.string.title));

            fragmentMain = new FragmentMain();

            android.app.FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                       .replace( R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
                       .commit();

        }
     }

当我点击项目时,一切正常。导航抽屉关闭不顺畅,但冻结(抽搐,抽搐),因为片段在后台重新加载。

导航抽屉的接近程度如何?

2 个答案:

答案 0 :(得分:9)

您必须决定是否更喜欢:

替换Fragment中的FrameLayout,然后关闭DrawerLayout

关闭DrawerLayout,然后替换您的Fragment

实施第一种方法:

只需在代码中进行更改:

listViewDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        toolbar.setTitle(getResources().getString(R.string.title));

        fragmentMain = new FragmentMain();

        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                   .replace(R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
                   .commit();

        fragmentManager.executePendingTransactions(); // wait for the transaction to finish
        drawerLayout.closeDrawer(listViewDrawer); // call closeDrawer(...) as a last thing
    }
 }

实施第二种方法:

在用户点击时设置某种标志:

listViewDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mDrawerItemClicked = true;
        drawerLayout.closeDrawer(listViewDrawer); 
    }
 }

并在DrawerLayout.DrawerListener上设置DrawerLayout,如下所示:

drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
    }

    @Override
    public void onDrawerOpened(View drawerView) {
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        if (mDrawerItemClicked){
            mDrawerItemClicked = false;

            toolbar.setTitle(getResources().getString(R.string.title));
            fragmentMain = new FragmentMain();
            android.app.FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                   .replace(R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
                   .commit();
        }
    }

    @Override
    public void onDrawerStateChanged(int newState) {
    }
});

答案 1 :(得分:2)

抽屉关闭大约需要200毫秒。所以你可以这样做:

Handler mHandler = new Handler();

drawerLayout.closeDrawer( listViewDrawer);
mHandler.postDelayed(
                new Runnable() {
                    @Override
                    public void run () {
                        loadFragment();
                    }
                }
                , NAVDRAWER_CLOSE_DELAY // 250...for delay
        );