在NavigationDrawer中切换多个ViewPagers

时间:2016-06-22 18:25:18

标签: android android-fragments android-studio android-viewpager android-navigation-drawer

我正在尝试构建一个带有导航抽屉的应用程序,其中包含多个菜单项,这些菜单项负责切换到片段,每个菜单项都有一个ViewPager。

假设我们有一个带有NavigationDrawer的MainActivity类,就像这样

public class MainActivity extends AppCompatActivity
{
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
private FragmentActivity fragment;
private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    MultiDex.install(getApplicationContext());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initDrawerNavigation();
    fragmentManager = getFragmentManager();
}

public void initDrawerNavigation()
{
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(R.string.app_name);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {

        @Override
        public boolean onNavigationItemSelected(MenuItem item)
        {
            item.setChecked(true);
            if(fragment != null)
                fragment.finish();

            switch (item.getItemId())
            {
                case R.id.st_menu:
                    fragment = new FirstMenu();
                    break;

                case R.id.nd_menu:
                    fragment = new SecondMenu();
                    break;

                case R.id.nav_settings:
                    Intent i = new Intent(getApplicationContext(), SettingsActivity.class);
                    startActivity(i);
                    break;
            }

            //fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

            mDrawerLayout.closeDrawers();
            return true;
        }
    });

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
            R.string.drawer_close)
    {
        // Called when a drawer has settled in a completely closed state.
        public void onDrawerClosed(View view)
        {
            super.onDrawerClosed(view);
        }

        // Called when a drawer has settled in a completely open state.
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.addDrawerListener(mDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId())
    {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

现在,FirstMenu班级和SecondMenu班级都有ViewPager。根据{{​​3}}在每个ViewPager中实施FragmentActivity,他们需要像这样扩展public class FirstMenu extends FragmentActivity { private ViewPager mPager; private PagerAdapter mPagerAdapter; private static final int NUM_PAGES = 2; private SecondPage secondPage; private FirstPage firstPage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPager = (ViewPager) findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); mPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { invalidateOptionsMenu(); } }); } @Override public void onBackPressed() { if (mPager.getCurrentItem() == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed(); } else { // Otherwise, select the previous step. mPager.setCurrentItem(mPager.getCurrentItem() - 1); } } private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch(position) { case 0: return firstPage= new FirstPage(); case 1: return secondPage= new SecondPage(); default: return firstPage = new FirstPage(); } } @Override public int getCount() { return NUM_PAGES; } } }

public class SecondMenu extends FragmentActivity
{
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 2;
private ThirdPage thirdPage;
private FourthPage fourthPage;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    mPager = (ViewPager) findViewById(R.id.pager2);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
        }
    });
}

@Override
public void onBackPressed()
{
    if (mPager.getCurrentItem() == 0)
    {
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.
        super.onBackPressed();
    }

    else
    {
        // Otherwise, select the previous step.
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        switch(position)
        {
            case 0:
                return thirdPage = new ThirdPage();
            case 1:
                return fourthPage = new FourthPage();
            default:
                return thirdPage = new ThirdPage();
        }
    }

    @Override
    public int getCount()
    {
        return NUM_PAGES;
    }
}
}

和此:

public class FirstPage extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.first_page, container, false);

    return view;
}
}

现在每个页面都是这样的:

fragment = new FirstMenu();

根据https://developer.android.com/training/animation/screen-slide.htmlHow to get items in Navigation Drawer to change view在菜单之间切换,我应该致电

FragmentActivity

但我不能这样做,因为两个菜单都延伸startActivity(newIntent(MainActivity.this, FirstMenu.class)); 。根据这个:How to change fragments using Android navigation drawer在Menus之间切换我应该打电话

NavigationDrawer

然后我从MainActivity中删除了def updateObjectWithId(self,objectId, objectJson): self.connection.connect() if(objectId == ""): self.connection.request('POST','/parse/classes/'+self._className,objectJson, { "X-Parse-Application-Id": self.appId, "X-Parse-Master-Key": self.masterApiKey, "Content-Type": "application/json" #This made it work }) else: print "ParseArchiver.updateObjectWithId: id: ", objectId, " of Class: ", self._className, " with json: ", objectJson self.connection.request('PUT','/parse/classes/'+self._className+'/'+objectId,objectJson, { "X-Parse-Application-Id": self.appId, "X-Parse-Master-Key": self.masterApiKey, "Content-Type": "application/json" #This made it work. }) result = json.loads(self.connection.getresponse().read()) time.sleep(ParseArchiver.ParseSleepTime) print "*** ParseArchiver.updateObjectWithId: Result object is:",result return ,因为我正在启动不包含一个的新Intent。除了应用程序崩溃。实施此类事情的正确方法是什么?

0 个答案:

没有答案