简体   繁体   English

按下项目后如何立即关闭导航抽屉?

[英]How to close navigation drawer immediately after an item is pressed?

I'm using navigation drawer working fine, but it closing slowly after an item is pressed. 我使用的导航抽屉工作正常,但在按下某个项目后它关闭缓慢。 This happens if next activity has a extra code in oncreate method otherwise working properly.. 如果下一个活动在oncreate方法中包含一个额外的代码,则此情况会发生,否则会正常工作。

So please help to solve this 所以请帮忙解决这个问题

I just managed to solve the problem you're experiencing. 我只是设法解决了您遇到的问题。

First of all i have to say that i'm working on Android Studio 1.1.0 generated NavigationDrawer project. 首先,我不得不说我正在处理Android Studio 1.1.0生成的NavigationDrawer项目。

This is the method onCreateView() of the class NavigationDrawerFragment 这是NavigationNavigatorerFragment类的onCreateView()方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);

    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            selectItem(position);
        }
    });

    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

    return mDrawerListView;
}

When a Item is clicked the 单击项目后,

mDrawerListView.setOnItemClickListener() mDrawerListView.setOnItemClickListener()

callback will fire and then the ball pass to 回调将触发,然后球传给

selectItem(position) selectItem(位置)

method. 方法。

The "selectItem()" method hides the NavigationDrawer and, using a callback, it calls a method into the "MainActivity" class - onNavigationDrawerItemSelected() -, that start the transition to the selected fragment. “ selectItem()”方法隐藏了NavigationDrawer,并使用回调将其调用到“ MainActivity”类中的方法-onNavigationDrawerItemSelected() -,该方法开始到所选片段的过渡。

The animation stuttering/lag happens because the code tries to close the NavigationDrawer and to get the UI layout hardcore job done at the same time. 发生动画卡顿/滞后是因为代码试图关闭NavigationDrawer并同时完成UI布局核心工作。

If you want to avoid the lag you have to choose what to do first. 如果要避免延迟,则必须先选择要做什么。

In my own workaround i decided to: 在我自己的解决方法中,我决定:

  1. Close the NavigationDrawer 关闭导航抽屉
  2. Getting the UI (layout) job done 完成UI(布局)作业

This is my own solution: 这是我自己的解决方案:

mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        final int pos = position;

        mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){
            @Override
            public void onDrawerClosed(View drawerView)
            {
                super.onDrawerClosed(drawerView);
                selectItem(pos);
            }
        });

        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
});

Don't be confused by the code. 不要被代码弄糊涂了。

We moved the selectItem(pos) inside a callback that will be fired only then the Drawer is closed and then we force the Drawer to close so the magic may occur. 我们将selectItem(pos)移到了仅在抽屉关闭后才触发的回调中,然后我们强制抽屉关闭,以免发生魔术。

This solution work for me, hope to know if it works for you as well. 该解决方案对我有用,希望知道它是否对您也有用。

There is closeDrawer(View view) and closerDrawer(int gravity). 有closeDrawer(视图视图)和closerDrawer(int重力)。 On your item click .closeDrawer(Gravity.START); 在您的项目上单击.closeDrawer(Gravity.START);

work fines for me.They might be helpful you too. 为我工作罚款。它们也可能对您有帮助。

in your displayView() you get the selected item from drawer so you can specify the mDrawerLayout gravity like displayView()您从抽屉中获取了选定的项目,因此您可以指定mDrawerLayout重力,例如

     private void displayView(int position) {
                // update the main content by replacing fragments
                Fragment fragment = null;
                switch (position) {
                case 0:
                    //load your fragment here
                    mDrawerLayout.closeDrawer(Gravity.LEFT);


                    break;
    }

on your item click you have to close drawaer layout 在项目上单击,您必须关闭抽屉布局

drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;

    case 1:
        fragment = new EventFragment();
        break;

    case 2:
        fragment = new SponsorsFragment();
        break;

    case 3:
        fragment = new AboutFragment();
        break;

    case 4:
        fragment = new OurTeamFragment();
        break;

    case 5:
        fragment = new VideoFragment();
        break;

    case 6:
        fragment = new ContactFragment();
        break;

    default:
        break;
    }
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
        // pushFragments(fragment, true, true);

        drawlayout.closeDrawers();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM