Navigation drawer highlighting

948 Views Asked by At

I have a navigation drawer and i use selector to get the highligghted item of the list

<item
    android:state_activated="true"
    android:drawable="@color/pressed_color"/>
<item
    android:drawable="@color/default_color" />

The problem is that i Override the backpressed to change my fragment to my main element like this:

@Override
public void onBackPressed()
{

    onNavigationDrawerItemSelected(0);
    onSectionAttached(1);
    restoreActionBar();

now when i press back it changes the fragment but the highlighted item stays where I clicked last time.How can I achive to change the highlights postion too? with somethig like change the texts state somehow from code,but i create the navigation drawer elements with an adapter ...(i use the Android Studio sample )

    @Override
public void onNavigationDrawerItemSelected(final int position) {
    // update the main content by replacing fragments
  final  FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, KedvencekFragment.newInstance(position + 1))
                    // .addToBackStack(null)
            .commit();
    if(position==0)
    {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentManager.beginTransaction()
                        .replace(R.id.container, KedvencekFragment.newInstance(position + 1))
                       // .addToBackStack(null)
                        .commit();
            }
        }, 250);

    }

and ` public void onSectionAttached(int number) { switch (number) { case 1: mTitle = getString(R.string.title_section1);

            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
        case 4:
            mTitle = getString(R.string.title_section4);
            break;
        case 5:
           // mTitle = getString(R.string.title_section5);
            break;
        case 6:
            mTitle = getString(R.string.title_section6);
            break;
    }
}`
2

There are 2 best solutions below

0
On BEST ANSWER

i found it!!! had to change the selectItem() to public then call it in the onBackPressed like this!

 @Override
public void onBackPressed()
{
    if(mTitle!=getString(R.string.title_section1)) {
        onNavigationDrawerItemSelected(0);
        onSectionAttached(1);

        mNavigationDrawerFragment.selectItem(0);

        restoreActionBar();
    }
    else{
        super.onBackPressed();
    }


}
8
On

EDIT:

You can access you NavigationDrawerFragment in your activity, you just need to make your selectItem(int position) method of the NavigationDrawerFragment public, like this:

...
publicvoid selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);

    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}
...

And then in your onNavigationDrawerItemSelected method:

@Override
public void onNavigationDrawerItemSelected(final int position) {
    // update the main content by replacing fragments
  final  FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, KedvencekFragment.newInstance(position + 1))
                    // .addToBackStack(null)
            .commit();
    if(position==0)
    {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentManager.beginTransaction()
                        .replace(R.id.container, KedvencekFragment.newInstance(position + 1))
                       // .addToBackStack(null)
                        .commit();
            }
        }, 250);

    }
    navigationDrawerFragment.selectItem(position);
}