按下项目后如何立即关闭导航抽屉? [英] How to close navigation drawer immediately after an item is pressed?

查看:24
本文介绍了按下项目后如何立即关闭导航抽屉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航抽屉工作正常,但在按下项目后它会慢慢关闭.如果下一个活动在 oncreate 方法中有额外的代码,否则会正常工作.

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..

所以请帮忙解决这个问题

So please help to solve this

推荐答案

我刚刚设法解决了您遇到的问题.

I just managed to solve the problem you're experiencing.

首先我必须说我正在开发Android Studio 1.1.0 生成的 NavigationDrawer 项目.

First of all i have to say that i'm working on Android Studio 1.1.0 generated NavigationDrawer project.

这是类NavigationDrawerFragment

@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;
}

当一个项目被点击时

mDrawerListView.setOnItemClickListener()

mDrawerListView.setOnItemClickListener()

回调将触发,然后球传给

callback will fire and then the ball pass to

选择项目(位置)

方法.

selectItem()"方法隐藏 NavigationDrawer,并使用回调调用MainActivity"类中的方法 -onNavigationDrawerItemSelected()-,开始转换到所选片段.

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.

出现动画卡顿/延迟是因为代码试图关闭 NavigationDrawer 并同时完成 UI 布局的核心工作.

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.

如果您想避免滞后,您必须先选择要做什么.

If you want to avoid the lag you have to choose what to do first.

在我自己的解决方法中,我决定:

In my own workaround i decided to:

  1. 关闭 NavigationDrawer
  2. 完成 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);
    }
});

不要被代码弄糊涂了.

我们将 selectItem(pos) 移动到一个回调中,该回调只有在 Drawer 关闭时才会触发,然后我们强制 Drawer 关闭,这样魔法可能会发生.

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.

这个解决方案对我有用,希望知道它是否也对你有用.

This solution work for me, hope to know if it works for you as well.

这篇关于按下项目后如何立即关闭导航抽屉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆