Navigation drawer doesn't close

后端 未结 11 964
暖寄归人
暖寄归人 2021-02-08 03:44

The navigation drawer in my app is not closing. I am using activities instead of fragments. When i click on any item in the listview, it opens other activities as i

相关标签:
11条回答
  • 2021-02-08 04:11

    use

    if(drawer.isDrawerOpen(navList))  
      {  
          drawer.closeDrawer(navList);      
      }
    

    In onResume() and start of onItemClick() method.

    or you can try another approach..run a Ui thread when you are selecting drawer item

    private void selectDrawerItemItem(final int position){
    
         //Toast.makeText(getApplicationContext(), "ItemClicked", Toast.LENGTH_SHORT).show();
    
         darwer.closeDrawer(navList);
    
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Fragment fragment = new Fragment(Activity.this);
                Bundle args = new Bundle();
                args.putInt(Fragment.ARG_PLANET_NUMBER, position);
                fragment.setArguments(args);
    
                FragmentManager fragmentManager = getSupportFragmentManager();
                 fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
    
                // update selected item and title, then close the drawer
                navList.setItemChecked(position, true);
    
                setTitle(" " + navListTitles[position]);
            }
        }, 200);
    
    
        // update the main content by replacing fragments
    
    
    }
    
    0 讨论(0)
  • 2021-02-08 04:12

    have you tried :

    mDrawerLayout.closeDrawer(drawerListView);
    

    You can add this before calling startActivity()

    0 讨论(0)
  • 2021-02-08 04:12

    You need to close the drawer on list item click

    drawer.closeDrawer(navList);
    

    Also what is the use of FrameLayout in your xml. It is not used as a container to add or replace fragments

    0 讨论(0)
  • 2021-02-08 04:12

    I was having the same problem.

    I used

    mDrawerLayout.closeDrawer(drawerListView);
    

    before starting my new activity. It beautifully slides the drawer back in.

    0 讨论(0)
  • 2021-02-08 04:18

    call the drawer.closeDrawer(navList); function before switch case

    0 讨论(0)
  • 2021-02-08 04:20

    In continuation to others answers and @ Chinmay Dabke question of 'but the drawer closes half then pauses and then closes fully' in one of the comments, here is what you could do:

    first as others suggested, this line is missing. drawer.closeDrawer(navList);

    And as far as the pausing of drawer is concerned while closing, you could do something like this. use a Runnable and a Handler like this:

    mRunnable = = new Runnable() {
        @Override
        public void run() {
             //say
             selectItem(pos); //Implement your switch case logic in this func
        }
    }
    

    and then in the onDrawerClosed overrided method

    @Override
    public void onDrawerClosed(View view) {
    
    if (mRunnable != null) {
        mHandler.post(mRunnable);
        mRunnable = null;
    }
    }
    

    Hope this helps!

    I would suggest you to use fragments for navigation drawer and to solve this issue of drawer not closing properly, I found this article very useful (using fragments). http://www.michenux.net/android-asynctask-in-fragment-best-pratices-725.html

    0 讨论(0)
提交回复
热议问题