Lack of navigation box on Android

I have a problem with the navigation box, it's too slow, the solution I'm looking for is to close the box first and then show the action, but it doesn’t work, of course, 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); } 
+18
java android fragmenttransaction navigation-drawer
Aug 27 '14 at 18:45
source share
7 answers

You can do this in order to avoid lagging the box, change onItemClick :

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

Edit: The preferred way is to set DrawerListener to DrawerLayout and set your fragment to onDrawerClosed for example:

 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; } } }); 
+37
Aug 27 '14 at 19:59
source share

Instead of a transaction in onClick, why not do it in onDrawerClosed from DrawerLayout.DrawerListener?

+19
Aug 27 '14 at 19:48
source share

I think I have found the best solution for this!

First, execute the fragment transaction as follows:

 new Handler().postDelayed(new Runnable() { @Override public void run() { getSupportFragmentManager() .beginTransaction() .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out) .replace(R.id.container, finalMenuFragment) .commit(); } }, 0); 

I know it is useless to do postDelayed with 0, but this hack avoids the lag in the animation of clicks on the box, especially when you use android:background="?attr/selectableItemBackground" for an interactive element.

And then you close the box at the end of the onResume () function of your fragment (in this example, "finalMenuFragment") as follows:

  new Handler().postDelayed(new Runnable() { public void run() { mDrawerLayout.closeDrawer(mFragmentContainerView); } }, 0); 

Again I know it seems silly postDelayed with 0, but it makes smooth animation smooth.

Thus, the box will close as quickly as possible, if you need smooth animations, and if your fragment does not have many views, it will close faster and without delay.

I would love to get some feedback on this if anyone has verified this solution, hope this helps!

+7
Sep 08 '15 at 10:58
source share

I just solved this problem only in One-Line

go to your Menifest file. In your Activity drawer, just set HardwareAccelerated true to prevent drawer latency.

 <activity android:name=".activity.YourDrawerActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" android:hardwareAccelerated="true" //important android:theme="@style/AppTheme.NoActionBar" android:windowSoftInputMode="stateHidden" /> 
+5
Dec 07 '17 at 5:24 on
source share

Try it. The box will be prompted to close using broadcast. This is to ensure that the activity or fragment is loaded 1st before closing the box.

Inside MainActivity

 private BroadcastReceiver xBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (Drawer.isDrawerOpen(GravityCompat.START)){ Drawer.closeDrawers(); } } }; @Override protected void onStart() { super.onStart(); IntentFilter filter = new IntentFilter(); filter.addAction("my.drawer.listener"); this.registerReceiver(xBroadcastReceiver, filter); } @Override public void onDestroy() { super.onDestroy(); this.unregisterReceiver(xBroadcastReceiver); } 

Internal activity or fragment

 @Override public void onStart() { super.onStart(); Intent intent = new Intent(); intent.setAction("my.drawer.listener"); getActivity().sendBroadcast(intent); } 

you can change it to the previous process from onCreate or onAttach, but it depends on your application.

+1
Jun 24 '15 at 7:38
source share

See this one for the seemingly correct combination of both of the above answers.

0
Sep 17 '14 at 18:47
source share

I think using delays for fragment transactions is the most volatile solution.

The one who makes them onDrawerClosed gives you a slight delay between closing and the appearance of the fragment.

I prefer to do transactions immediately after clicking on the link, close the box using postDelayed 200ms (in the runnable check in case it is not null). The fragment opens before the box begins to close.

Not only the delay from pressing to closing the drawer is shorter than after closing the drawer, and the following fragment appears, it is better to UX when the user removes a finger from the screen - less obvious.

0
Nov 22 '17 at 10:13
source share



All Articles