调用closeDrawers当导航抽屉不会关闭 - 计时问题 [英] Navigation drawer does not close when calling closeDrawers - timing issue

查看:1386
本文介绍了调用closeDrawers当导航抽屉不会关闭 - 计时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击导航抽屉里的列表中的项目何时关闭抽屉式导航栏。我有2个活动的和下方code B的是A和B是相同的:

I am trying to close the navigation drawer when an item in the navigation drawer's list is clicked. I have 2 activities A and B. The below code is the same for both A and B:

final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);

// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        Intent intent = null;

        // first just close the drawer
        mDrawerLayout.closeDrawers();

        if (position == 0) { // Activity A
            intent = new Intent(v.getContext(), ActivityA.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
        else if (position == 1) { // Activity B
            intent = new Intent(v.getContext(), Activity B.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }
});

我想要做的就是关闭抽屉式导航栏,当用户从活动导航离开,这样,当他回来后,抽屉被关闭,不留开了。

What I want to do is close the navigation drawer when the user navigate away from the activity so that when he comes back, the drawer is closed and does not stay opened.

不过,上述code不工作:

However, the above code doesn't work:


  • 如果我在活动A,和我去的活动B,再从B I回A(所有这些都是通过导航抽屉中),在一个抽屉里保持打开

  • If I'm in activity A, and I go to activity B, then from B I go back to A (all via the navigation drawer), the drawer in A stays opened

但如果我在活动A,我点击了酶活性的项目,我看到抽屉关闭就好了

but if I'm in activity A, and I click on the item for activity A, I see the drawer closed just fine

我试着做一些记录:

/** Called when a drawer has settled in a completely closed state. */
@Override
public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    Log.i("foo", "drawer closing");
}

/** Called when a drawer has settled in a completely open state. */
@Override
public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
    Log.i("foo", "drawer opening");
}

和我发现:


  • 如果我在一个活动,我点击该项目在抽屉里的活动,抽屉将关闭

  • if I am in an activity and I click on the item for that activity in the drawer, the drawer will close

但如果我去另一个活动,抽屉将不会关闭

but if I go to another activity, the drawer will NOT close

我发现这很奇怪,所以我认为这也许是时间问题。所以,我打过电话后, closeDrawers()在做任何事情之前(即不断变化的活动),以等待1秒:

I found this very strange, so I thought it maybe a timing issue. So I tried to wait for 1 second after calling closeDrawers() before doing anything else (i.e. changing activity):

// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, final View v, final int position, long arg3) {

        // first just close the drawer
        mDrawerLayout.closeDrawers();

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = null;
                if (position == 0) { // Activity A
                    intent = new Intent(v.getContext(), ActivityA.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
                else if (position == 1) { // Activity B
                    intent = new Intent(v.getContext(), ActivityB.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
            }
        }, 1000);
    }
});

果然,抽屉成功关闭。

and sure enough, the drawer closed successfully.

我的问题是:


  • 这是怎么回事?我认为他们(关闭抽屉里,不断变化的活性)在同一线程依次所有运行因此必须先于另一个完成可以执行?

  • 我怎样才能确保在执行之前别的导航抽屉关闭?

推荐答案

因为你提到它是关于时间问题,在 startActivity 将被执行比关闭<$ C快$ C> NavigationDrawer 。关于你的第二个问题,你可以在 DrawerListener 为后您的导航闭完全启动你的第二个活动设置:

as you mention it's about timing issue , the startActivity would be execute faster than closing the NavigationDrawer.about your second question you can set a DrawerListener for that and after your Navigation Closed completely start your second Activity :

mDrawerLayout.setDrawerListener(new DrawerListener() {

        @Override
        public void onDrawerStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerSlide(View arg0, float arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerOpened(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerClosed(View arg0) {

            GotoActivity();
        }
    });

这篇关于调用closeDrawers当导航抽屉不会关闭 - 计时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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