当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Toggle button not working for navigation drawer for the first time

问题描述:

I saw similar questions but answers are no use. That's why I post it.

Here is how I setup my Drawer at FragmentDrawer.java.

public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {

containerView = getActivity().findViewById(fragmentId);

mDrawerLayout = drawerLayout;

toolBar = toolbar;

onDrawerOpen();

mDrawerToggle.setDrawerIndicatorEnabled(false);

mDrawerToggle.setHomeAsUpIndicator(R.drawable.drawer);

mDrawerLayout.setDrawerListener(mDrawerToggle);

mDrawerLayout.post(new Runnable() {

@Override

public void run() {

mDrawerToggle.syncState();

}

});

mDrawerToggle.setToolbarNavigationClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

onDrawerOpen();

}

});

}

Here is onDrawerOpen method.

private void onDrawerOpen() {

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,

toolBar, R.string.drawer_open, R.string.drawer_close) {

@Override

public void onDrawerOpened(View drawerView) {

super.onDrawerOpened(drawerView);

getActivity().invalidateOptionsMenu();

}

@Override

public void onDrawerClosed(View drawerView) {

super.onDrawerClosed(drawerView);

getActivity().invalidateOptionsMenu();

}

@Override

public void onDrawerSlide(View drawerView, float slideOffset) {

super.onDrawerSlide(drawerView, slideOffset);

toolBar.setAlpha(1 - slideOffset / 2);

}

};

}

And call from MainActivity like this.

drawerFragment.setUp(R.id.fragment_navigation_drawer, mDrawer, mToolbar);

网友答案:

After you first initialize the ActionBarDrawerToggle by calling onDrawerOpen(), you call setDrawerIndicatorEnabled(false), which disables the indicator, and causes any clicks to be routed to the Navigation click listener. Upon the first click, onDrawerOpen() is called again, which re-initializes the toggle, and it then works as expected.

It's unclear exactly what you're trying to do, but if you just want the standard toggle behavior, move the toggle initialization to the setup() method, and remove the setDrawerIndicatorEnabled() and setToolbarNavigationClickListener() calls.

public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
    containerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    toolBar = toolbar;

    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
                                              toolBar, R.string.drawer_open,
                                              R.string.drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            toolBar.setAlpha(1 - slideOffset / 2);
        }
    };

    mDrawerToggle.setHomeAsUpIndicator(R.drawable.drawer);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: