viewpager with navigationdrawer

耗尽温柔 提交于 2019-12-24 21:56:52

问题


i'm trying to implement fragments with and without viewpager inside a navigation drawer. here's what I'm trying to achieve:

1) an app with navigation drawer, with 3 menus: dashboard, expenses, income

2) 'dashboard' is just a normal fragment with one page (or what should I call it) and NO viewpager

3) expenses & income are another fragments which contains viewpager; expenses have 2 viewpager pages (daily, recurring), while income have 3 viewpager pages (daily,recurring,report)

so here's a portion of my code (I just modified the code that Android Studio beta generates when we select a NavigationDrawer project):

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;

            }
        }
    }
}

but I still haven't figure out how to implement expenses.java which are being called when we select 'expenses' menu from NavigationDrawer. Expenses.java should implement ViewPager, and it should have 2 pages, which is Daily & Recurring. Any help are much appreciated. thanks

update #1:

I tried to make Expenses.java extends FragmentActivity but it seems we can't call another activity from NavigationDrawer using the code:

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

                default:
                    break;
            }

even if it could be done, that's not what I want because opening another activity is like opening a new window, right?


回答1:


am also working on same requirement here is my demo code hopefully it will help you in your development phase

i was also stucked almost a day then after so much of research finally i tackled the issue and achieved which i want

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




回答2:


The part you are having trouble with is that you want some of your top-level Fragments, namely ExpensesFragment and IncomeFragment, to use more fragments for the pages of the ViewPager (since you've already defined a FragmentPagerAdapter implementation).

When you instantiate the FragmentPagerAdapter, you give it a FragmentManager. I believe you need to use getChildFragmentManager() in the top-level Fragment, for example like so:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    MyPageAdapter adapter = new MyPageAdapter(getChildFragmentManager());
    viewPager.setAdapter(adapter);
}

This will provide a FragmentManager instance specific to this particular Fragment and used for managing nested fragments.



来源:https://stackoverflow.com/questions/24903309/viewpager-with-navigationdrawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!