0

I'm trying to use a NavigationDrawer with FragmentStatePagerAdapter for the navigation of my app. The idea is that when clicking on an item of the NavigationDrawer a new fragment opens with two tabs in it.

My problem is that I can't make it work correctly. When clicking on an item of the NavigationDrawer, instead of two tabs I get four, then six, ...

This problem can be solved with actionBar.removeAllTabs(), but my main problem is that when I come back on a previous fragment by re-clicking on the item of the NavigationDrawer (fragment 1 -> fragment 2 -> fragment 1), I got an empty fragment.

I tried to solve this problem by make some changes according to what's stated on other pages, but I didn't succeed in making it work.

Any help would be appreciated, thank you!

Here is the class managing my main fragment:

public class FragmentMultiTab extends SherlockFragment {

private MyVariables mk;
private ActionBar actionBar;
private ViewPager viewPager;
private View rootView;
private int i;

@Override
public View onCreateView(final LayoutInflater inflater,
        final ViewGroup container, final Bundle savedInstanceState) {

    final int[] fragment = { R.layout.fragment1, R.layout.fragment2,
            R.layout.fragment3 };
    final int[] pager = { R.id.pager1, R.id.pager2, R.id.pager3 };

    this.mk = new MyVariables(this.getArguments());
    i = this.mk.getInt("INDEX");

    this.rootView = inflater.inflate(fragment[i], container, false);
    viewPager = (ViewPager) this.rootView.findViewById(pager[i]);
    viewPager.setOnPageChangeListener(onPageChangeListener);
    viewPager.setAdapter(new ViewPagerAdapter(getFragmentManager()));
    viewPager.getAdapter().notifyDataSetChanged();
    addActionBarTabs();

    return rootView;
}

private final ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        super.onPageSelected(position);
        actionBar.setSelectedNavigationItem(position);
    }
};

private void addActionBarTabs() {
    actionBar = getSherlockActivity().getSupportActionBar();

    final String[][] tabTitle = {{"Tab 1","Tab 3","Tab 5" },{"Tab 2","Tab 4",
                    "Tab 6" } };

    for (int k = 0; k < 2; k++) {
        ActionBar.Tab tab = actionBar.newTab().setText(tabTitle[k][i])
                .setTabListener(tabListener);
        actionBar.addTab(tab);
    }

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}

private final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
            }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
};
 }

Here is the ViewPager used

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    final int PAGE_COUNT = 2;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
    }

@Override
public Fragment getItem(int arg0) {

    switch (arg0) {
    case 0:
        FragmentTab1 fragmenttab1 = new FragmentTab1();
        return fragmenttab1;
    case 1:
        FragmentTab2 fragmenttab2 = new FragmentTab2();
        return fragmenttab2;
    }
    return null;
}

@Override
public int getItemPosition(Object object) {
    return PagerAdapter.POSITION_NONE;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}

}

My fragments have the following structure:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

</RelativeLayout>

And finally here is my MainActivity:

public class MainActivity extends SherlockFragmentActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
FragmentMultiTab fragment1 = new FragmentMultiTab();
FragmentMultiTab fragment2 = new FragmentMultiTab();
FragmentMultiTab fragment3 = new FragmentMultiTab();
private final MyVariables mk = new MyVariables();
private int currentPosition;

@Override
public void onConfigurationChanged(final Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    this.mDrawerToggle.onConfigurationChanged(newConfig);
}

// The click listener for ListView in the navigation drawer
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

    @Override
    public void onItemClick(final AdapterView<?> parent, final View view,
            final int position, final long id) {
        MainActivity.this.selectItem(position);
    }
}

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_main);

    currentPosition = -1;

    // Locate DrawerLayout in activity_main.xml
    this.mDrawerLayout = (DrawerLayout) this
            .findViewById(R.id.drawer_layout);

    // Locate ListView in activity_main.xml
    this.mDrawerList = (ListView) this.findViewById(R.id.listview_drawer);

    // Set a custom shadow that overlays the main content when the drawer
    // opens
    this.mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    // Pass results to MenuListAdapter Class
    this.mMenuAdapter = new MenuListAdapter(this);

    // Set the MenuListAdapter to the ListView
    this.mDrawerList.setAdapter(this.mMenuAdapter);

    // Capture button clicks on side menu
    this.mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    this.getSupportActionBar().setHomeButtonEnabled(true);
    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    this.mDrawerToggle = new ActionBarDrawerToggle(this,
            this.mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {

        @Override
        public void onDrawerClosed(final View view) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(view);
        }

        @Override
        public void onDrawerOpened(final View drawerView) {
            // TODO Auto-generated method stub
            super.onDrawerOpened(drawerView);
        }
    };

    this.mDrawerLayout.setDrawerListener(this.mDrawerToggle);

    if (savedInstanceState == null) {
        this.selectItem(0);
    }

    this.mk.setVariables(this);

}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    this.getSupportMenuInflater().inflate(R.menu.action_bar_main, menu);
    this.getActionBar().setDisplayHomeAsUpEnabled(true);
    return true;
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {

    switch (item.getItemId()) {
    case android.R.id.home:

        if (this.mDrawerLayout.isDrawerOpen(this.mDrawerList)) {
            this.mDrawerLayout.closeDrawer(this.mDrawerList);
        } else {
            this.mDrawerLayout.openDrawer(this.mDrawerList);
        }
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onPostCreate(final Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    this.mDrawerToggle.syncState();
}

private void selectItem(final int position) {

    final FragmentTransaction ft = this.getSupportFragmentManager()
            .beginTransaction();

    if (currentPosition != position)

        // Locate Position
        switch (position) {
        case 0:
            this.mk.addInt("INDEX", 0);
            this.mk.addBoolean("START", false);
            this.fragment1.setArguments(this.mk.getArgs());
            ft.replace(R.id.content_frame, this.fragment1);
            break;

        case 1:
            this.mk.addInt("INDEX", 1);
            this.fragment2.setArguments(this.mk.getArgs());
            ft.replace(R.id.content_frame, this.fragment2);
            break;

        case 2:
            this.mk.addInt("INDEX", 2);
            this.fragment3.setArguments(this.mk.getArgs());
            ft.replace(R.id.content_frame, this.fragment3);
            break;
        }

    currentPosition = position;

    ft.commit();
    this.mDrawerList.setItemChecked(position, true);
    // Close drawer
    this.mDrawerLayout.closeDrawer(this.mDrawerList);
}


}
MikeB
  • 1
  • 1

1 Answers1

0

I'd suggest using the ViewPagerIndicator instead of the standard ActionBar Tabs (they have some issues with regards to being converted into dropdown menus in some cases. The interface is quite simply (you just hook your viewpagerindicator to a viewpager), and you'll probably solve your issue as well.

Tas Morf
  • 3,065
  • 1
  • 12
  • 8