Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for Navigation Drawer to close before initializing Fragments with empty while-loop

According to the DrawerLayout documentation, "Avoid performing expensive operations such as layout during animation as it can cause stuttering".

Thus, I've tried waiting for the drawer to close before proceeding:

@Override public void onItemClick(AdapterView parent
                                , View view
                                , int position
                                , long id) {
    // Close the drawer
    mDrawerLayout.closeDrawer(mDrawerList);

    ExecutorService es = Executors.newSingleThreadExecutor();
    final int mPosition = position;
    Thread navThread = new Thread(new Runnable() {
        @Override public void run() {

            // Wait for the drawer to close
            while (mDrawerLayout.isDrawerOpen(mDrawerList));

            // Initialize the Fragments.
            runOnUiThread(new Runnable() {
                @Override public void run() {
                    selectNavigationItem(mPosition);
                }
            });
        }
    });
    es.submit(navThread);
    es.shutdown();
}

Even though it works, Lint warns me about the empty while loop. How can I achieve this effect without the empty while-loop?

like image 850
iamreptar Avatar asked Nov 19 '13 22:11

iamreptar


1 Answers

In the ListView.OnItemClickListener:

@Override public void onItemClick(AdapterView parent
                            , View view
                            , int position
                            , long id) {
    mPosition = position;
    mNavigationItemClicked = true;
    mDrawerLayout.closeDrawer(mDrawerList); 
}

In the DrawerLayout:

public void onDrawerClosed(View view) {
    getSupportActionBar().setTitle(mTitle);
    if (mNavigationItemClicked)
        selectNavigationItem();
    mNavigationItemClicked = false;
}

In selectNavigationItem():

switch (mPosition) {
    ...
}
like image 152
iamreptar Avatar answered Sep 19 '22 15:09

iamreptar