Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer lag on Android

Tags:

I'm having a problem with Navigation Drawer , it is too slow, the solution I'm looking for is to close the drawer first and then show the activity, but It is not working, certainly I'm missing something.

private class DrawerItemClickListener implements ListView.OnItemClickListener {         @Override         public void onItemClick(AdapterView<?> parent, View view, int posicao, long id) {             setLastPosition(posicao);             setFragmentList(lastPosition);             layoutDrawer.closeDrawer(linearDrawer);         }     }      private OnClickListener userOnClick = new OnClickListener() {         @Override         public void onClick(View v) {             layoutDrawer.closeDrawer(linearDrawer);         }     };      private void setFragmentList(int posicao) {          FragmentManager fragmentManager = getSupportFragmentManager();         Fragment fragment = new FragmentViagens();          switch (posicao) {              case 0:                 fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();                 break;             case 1:                 fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentPedidos()).commit();                 break;             case 2:                 fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentClientes()).commit();                 break;          }          navigationAdapter.setChecked(posicao, true);         setTitleFragments(lastPosition);         navigationAdapter.resetarCheck();         layoutDrawer.closeDrawer(linearDrawer);      } 
like image 299
AND4011002849 Avatar asked Aug 27 '14 18:08

AND4011002849


People also ask

What is navigation drawer activity in Android?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

Why do we use the navigation drawer in Android app?

Navigation drawers provide access to destinations and app functionality, such as switching accounts. They can either be permanently on-screen or controlled by a navigation menu icon. Navigation drawers are recommended for: Apps with five or more top-level destinations.

What is drawer layout in Android?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.


2 Answers

You can do It this way to avoid drawer lag, change your onItemClick:

layoutDrawer.closeDrawer(linearDrawer); setLastPosition(posicao); new Handler().postDelayed(new Runnable() {         @Override         public void run() {             setFragmentList(lastPosition);         }     }, 200); 

Edit: prefered way should be setting DrawerListener on DrawerLayout and setting your fragment in onDrawerClosed like this:

Fragment mFragmentToSet = null;  @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {     // Handle navigation view item clicks here.     switch (item.getItemId()) {         case R.id.nav_home:             mFragmentToSet = HomeFragment.newInstance();              break;     }      mDrawerLayout.closeDrawer(GravityCompat.START);     return true; } 

mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {         @Override public void onDrawerSlide(View drawerView, float slideOffset) {}         @Override public void onDrawerOpened(View drawerView) {}         @Override public void onDrawerStateChanged(int newState) {}          @Override         public void onDrawerClosed(View drawerView) {           //Set your new fragment here           if (mFragmentToSet != null) {             getSupportFragmentManager()                   .beginTransaction()                   .replace(FRAGMENT_CONTAINER_ID, mFragmentToSet)                   .commit();             mFragmentToSet = null;           }         }     }); 
like image 134
Yuraj Avatar answered Oct 18 '22 14:10

Yuraj


Rather than do transaction in onClick, why not do it in onDrawerClosed from DrawerLayout.DrawerListener?

like image 31
uDevel Avatar answered Oct 18 '22 12:10

uDevel