如何一个项目后立即关闭导航抽屉pressed?抽屉、项目、pressed

2023-09-04 06:52:48 作者:男人看胸不看心

我使用的是抽屉式导航工作正常,但它的一个项目之后慢慢关闭是pssed $ P $。这种情况如果下一个活动有一个额外的code在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..

所以,请帮忙解决这个

推荐答案

我只是设法解决您所遇到的问题。

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

首先我必须说,我工作的 Android的工作室1.1.0 产生NavigationDrawer项目。

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

这是该方法的 onCreateView()之类的 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

选择信息(位置)

方法。

在选择信息()方法隐藏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.

动画口吃/滞后是因为code试图关闭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:

关闭NavigationDrawer 使用用户界面(布局)完成任务

这是我自己的解决方案:

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

不要由code相混淆。

Don't be confused by the code.

我们感动的选择信息(POS)只会迫使我们的抽屉关闭,可能会出现的神奇。被解雇那么抽屉被关闭,然后回调里面

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.