【问题标题】:Android: tap on action bar doesn't open navigation drawerAndroid:点击操作栏不会打开导航抽屉
【发布时间】:2014-10-10 13:38:24
【问题描述】:

受到(default project inside android studio) 和各种网站的启发,我想实现导航抽屉作为布局和代码的片段。但当时点击时打开导航抽屉的动作操作栏不起作用。 为什么默认项目有效? 代码几乎相同。 代码:

public class NavigationDrawerFragment extends Fragment {
/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 */
public void setUp(int fragmentId, View drawerLayout) {
    Log.d(TAG,"setUp started");
    if (drawerLayout instanceof DrawerLayout) {//if view smartphone
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = (DrawerLayout)drawerLayout;

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the navigation drawer and the action bar app icon.
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActionBar().setTitle(getString(R.string.app_name));
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(getString(R.string.visualing_when_drawer_open));

                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                }

            }
        };

        // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
        // per the navigation drawer design guidelines.
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

“主要活动”:

public class MyActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

private final String TAG = getClass().getSimpleName();

private boolean doubleBackToExitPressedOnce;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, findViewById(R.id.drawer_layout));//TODO error for tablet (not DrawerLayout but Linear)
}

如果你想要所有项目都可以在github 页面上找到(分支开发)。


第一次尝试:我尝试在 navigationdrawerfragment 类上设置它,但不起作用

@Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDrawerToggle.syncState();
}

第二次尝试:我尝试在“MainActivity”中设置它,但不起作用

@Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            NavigationDrawerFragment.mDrawerToggle.syncState();
        }

【问题讨论】:

    标签: android android-actionbar navigation-drawer


    【解决方案1】:

    将此添加到您的 NavigationDrawerFragment:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // If the drawer is open, show the global app actions in the action bar. See also
        // showGlobalContextActionBar, which controls the top-left area of the action bar.
        if (mDrawerLayout != null && isDrawerOpen()) {
            inflater.inflate(R.menu.global, menu);
            showGlobalContextActionBar();
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    在此到onDrawerOpened末尾的setup方法:

    getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
    

    你的代码中缺少这个,添加这个,它可能会起作用

    【讨论】:

    • 是的,它们不存在,但正如您在 github 链接中看到的那样,这不是一个活动,而是一个片段。我该怎么办?
    • @mcmaur 我认为您至少需要在活动代码中添加一些内容。您可以从片段中做很多事情,但像 onPostCreate 应该在活动中
    • 正如您在我编辑的答案中看到的那样,它不起作用。第二个应该是填充选项菜单,但我不需要它。事实上,如果你看看 android studio [link]dropbox.com/sh/x60mctifz2o76wf/AACw_sHVyjwUjmJ2DXXmT_VAa?dl=0) 中的默认应用程序,没有。
    • @mcmaur 我改了答案,看看吧。这些是您的代码中缺少的东西。
    • 我认为这根本不相关,因为它是右上角菜单的设置(三个点)。事实上,我必须创建菜单 xml。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多