Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between tab navigation and list navigation

I am using ActionBarSherlock and applying this pattern for the tab navigation that I found on android developer site. It's working pretty good but I also want to able to switch between NAVIGATION_MODE_TABS and NAVIGATION_MODE_LIST preserving the association between tabs and the fragments.

The pattern I mentioned above is pretty good for preserving a generic code. So I add listeners to my tabs and associate them with specific fragments like this:

bar.addTab(bar.newTab()
.setText("MyFragment")
.setTabListener(new TabListener<SomeFragment>(this, "myfargment", SomeFragment.class)));

and instantiate the fragment when the associated tab is clicked with the help of generics:

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if (mFragment == null) {
        mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        ft.attach(mFragment);
    }
}

My question is how can I achieve a similar way while navigating between my fragments with the list navigation mode. I couldn't find a similar way since the OnNavigationListener for the list on the ActionBar works for the whole list instead of per item basis like the tablistener.

or do I have to do something like this:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch (itemPosition) {
    case 0:
    //Replace the current fragment with FragmentA
        break;
    case 1:
    //Replace the current fragment with FragmentB
        break;
    case 2:
    //Replace the current fragment with FragmentC
        break;
    default:
        break;
    }
    return true;
}

EDIT:

I have noticed an interesting behaviour: While the navigation mode is set to NAVIGATION_MODE_TABS if I put my phone in landscape mode it converts the tabs to a list and preservers the association between the fragments and the list items(which were tab items before) how can I achieve this result on demand rather than on orientation change?

like image 547
C.d. Avatar asked Sep 04 '26 08:09

C.d.


1 Answers

I dont think its possible if you are in tab mode to manually set to a list navigation. I have the list navigation set for one of my applications when it falls below the "Large" bucket. This is how I am using the navigation listener:

OnNavigationListener mNavigationListener = new OnNavigationListener()
{
int mLastPosition = -1;

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
    String selectedTag = mFragmentNames.get(itemPosition);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentByTag(selectedTag);
    FragmentTransaction ft = fm.beginTransaction();

    /** Used to avoid the same fragment being reattached. */
    if(mLastPosition != itemPosition)
    {
    /** Means there was a previous fragment attached. */
    if(mLastPosition != -1)
    {
        Fragment lastFragment = fm.findFragmentByTag(mFragmentNames.get(mLastPosition));
        if(lastFragment != null)
        ft.remove(lastFragment);
    }

    if(fragment == null)
    {
        /** The fragment is being added for the first time. */
        fragment = Fragment.instantiate(HomeActivity.this, selectedTag);
        ft.add(R.id.rootLayout, fragment, selectedTag);
        ft.commit();
    } else
    {
        ft.attach(fragment);
        ft.commit();
    }
    }
    /**
     * The newly attached fragment will be the last position if changed.
     */
    mLastPosition = itemPosition;

    return true;
}
};

The variable mFragmentNames is a HashMap that maps a integer position to a fragment name ex. "com.android.myproject.MyFragment"

I hope this helps.

like image 172
Marco RS Avatar answered Sep 07 '26 12:09

Marco RS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!