带导航抽屉的viewpager

时间:2014-07-23 06:28:28

标签: android android-viewpager navigation-drawer

我正在尝试在导航抽屉中使用和不使用viewpager实现片段。这就是我想要实现的目标:

1)带有导航抽屉的应用程序,带有3个菜单:仪表板,费用,收入

2)'仪表板'只是一个普通的片段,有一个页面(或我应该叫它)和没有viewpager

3)费用&收入是包含viewpager的另一个片段;费用有2个viewpager页面(每日,经常性),而收入有3个viewpager页面(每日,经常性,报告)

所以这是我代码的一部分(我刚刚修改了Android Studio beta在选择NavigationDrawer项目时生成的代码):

MyActivity.java
public class MyActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }
}

NavigationDrawerFragment.java
public class NavigationDrawerFragment extends Fragment {

private void selectItem(int position) {
        Fragment fragment = null;
        mCurrentSelectedPosition = position;

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

        switch (position) {
            case 0:
                fragment = new Dashboard();
                break;
            case 1:
                fragment = new Expenses();
                break;
            case 2:
                fragment = new Income();
                break;

            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

        } else {
            Log.e("NavigationDrawerFragment.java", "Error in creating fragment");
        }
    }
}

Expenses.java
public class Expenses extends Fragment{

private class MyPageAdapter extends FragmentPagerAdapter{
        private final String[] TITLES = { "Daily", "Recurring"};
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return ExpensesDaily();
                case 1:
                   return ExpensesRecurring;

            }
        }
    }
}

但我仍然没有弄清楚如何实现在我们从NavigationDrawer中选择“费用”菜单时调用的expenses.java。 Expenses.java应该实现ViewPager,它应该有2个页面,即Daily&定期。任何帮助都非常感谢。感谢

更新#1:

我试图让Expenses.java扩展FragmentActivity,但似乎我们无法使用代码从NavigationDrawer调用另一个活动:

switch (position) {
                case 0:
                    fragment = new Dashboard();
                    break;
                case 1:
                    fragment = new Expenses();
                    break;
                case 2:
                    fragment = new Income();
                    break;

                default:
                    break;
            }

即使可以做到,这也不是我想要的,因为打开另一个活动就像打开一个新窗口一样,对吗?

2 个答案:

答案 0 :(得分:0)

我也在制定相同的要求,这是我的演示代码,希望它能帮助你进入开发阶段

我也被困了差不多一天然后经过这么多研究后我终于解决了这个问题并实现了我想要的

https://www.dropbox.com/s/jc4tb636qr3vfc7/vikrant_pager_tabstrip.zip

答案 1 :(得分:0)

您遇到问题的部分是您希望某些顶级片段(即ExpensesFragment和IncomeFragment)为ViewPager的页面使用更多片段(因为您已经定义了{ {1}}实施)。

当您实例化FragmentPagerAdapter时,您会给它FragmentPagerAdapter。我认为您需要在顶级FragmentManager中使用getChildFragmentManager(),例如:

Fragment

这将提供特定于此特定片段的FragmentManager实例,并用于管理嵌套片段。