📜 ⬆️ ⬇️

As I lagged Navigation Drawer fought

Hi, Habr!

My name is Aleksey. I am developing for Android. Debugging in an emulator is like death, so I use my HTC Desire HD. The animal is already very ancient, for which I cannot dislike it, because any roughness and unevenness in the application on it are given to glorious lags. By the way, I highly recommend running your projects on medium-power devices, because not all users have flagships. So, while working on my new application, I found that when switching between fragments through Navigation Drawer, the navigation curtain noticeably goes down. When creating a fragment, requests were made to the database and loaded by SharedPreferences. I was just disgusted to watch this lag, and I figured out how to get rid of it. All who are interested, please under the cat.

I created a simple project with NavigationDrawer and three fragments (link to Github at the end of the article). The code does not claim to be ideal, I tried to write as simply and clearly as possible. The curtain code is taken directly from Google's examples . I solved the problem very simply and in the forehead: the download of the fragment starts in a separate thread and is delayed by 0.3 seconds (the magic number chosen experimentally).

First we hang the listener on the elements of the list like this:
')
mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 


And the selectItem method was:

 private void selectItem(int position) { Fragment fragment = new ContentFragment(); Bundle args = new Bundle(); args.putInt("positions", position); fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); mDrawerList.setItemChecked(position, true); setTitle(leftDrawerTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } 


And to remove the lag, we change these two places:

1) From selectItem, let's move all the code for selecting the active item and closing the Drawer to onCreate. selectItem will be like this:

 private void selectItem(int position) { Fragment fragment = new ContentFragment(); Bundle args = new Bundle(); args.putInt("positions", position); fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); } 


2) And in onCreate, we hang the listener on the Drawer elements and start the stream:

 mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) { mDrawerList.setItemChecked(position, true); setTitle(leftDrawerTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); //          0.3 , //      new Thread(new Runnable() { public void run() { try { TimeUnit.MILLISECONDS.sleep(300); selectItem(position); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }); 


That's all. Maybe this is a ridiculous crutch, but I was still very happy to open it for myself.

PS I would like to add about debugging on the phone. I highly recommend using ADB Over Wi-Fi (requires root rights). Wi-Fi does not limit you in movements and in turns of the device.
PPS I really want to get constructive criticism and advice from knowledgeable people.

Repository link: https://github.com/Rozag/Lags-free-navigation-drawer

Source: https://habr.com/ru/post/220835/


All Articles