无法制作导航抽屉和FragmentStatePagerAdapter一起工作

时间:2014-04-04 12:16:30

标签: android tabs actionbarsherlock navigation-drawer fragmentstatepageradapter

我尝试使用带有FragmentStatePagerAdapter的NavigationDrawer来导航我的应用。我们的想法是,当点击NavigationDrawer的一个项目时,会打开一个新的片段,其中包含两个标签。

我的问题是我无法使其正常工作。当点击NavigationDrawer的一个项目,而不是两个标签,我得到四个,然后六个,...

这个问题可以通过actionBar.removeAllTabs()解决,但我的主要问题是,当我通过重新点击NavigationDrawer的项目(片段1 - >片段2 - >片段1),我得到一个空片段。

我试图通过根据其他页面上的说明进行一些更改来解决这个问题,但我没有成功地使其发挥作用。

任何帮助将不胜感激,谢谢!

这是管理我的主要片段的类:

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) {
    }
};
 }

以下是使用的ViewPager

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;
}

}

我的片段具有以下结构:

<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>

最后这是我的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);
}


}

1 个答案:

答案 0 :(得分:0)

我建议使用ViewPagerIndicator而不是标准的ActionBar标签(在某些情况下,它们在转换为下拉菜单方面存在一些问题。界面非常简单(你只是勾住你的viewpagerindicator)到一个viewpager),你也可以解决你的问题。