2014-07-23 67 views
0

我試圖在導航抽屜內部實現具有和沒有viewpager的片段。這裏就是我想要實現:viewpager with navigationdrawer

1)與抽屜式導航應用,而在3個菜單:儀表盤,費用,收入

2)「儀表盤」只是一個頁面一個正常的片段(或者是什麼我應該叫它)和NO viewpager

3)費用&收入是另一個包含viewpager的片段;費用有2個viewpager頁面(每日,定期),而收入有3個viewpager頁面(每日,定期,報告)

所以這裏是我的代碼的一部分(我只是修改了Android Studio測試版, 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; 

      } 
     } 
    } 
} 

,但我還沒有搞清楚如何實現expenses.java正在被當我們選擇NavigationDrawer「費用」菜單調用。 Expenses.java應該實現ViewPager,並且它應該有2個頁面,即每日&反覆出現。任何幫助,非常感謝。感謝

更新#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; 
      } 

即使它可能是這不是我想要的,因爲打開另一個活動就像打開一個新窗口,對嗎?這裏

+0

這會幫助你。我有同樣的問題。這對我工作http://stackoverflow.com/a/24652563/3690664d – Madhav

回答

0

您有麻煩的部分是,你希望自己的某些頂級片段,即ExpensesFragment和IncomeFragment,多采用碎片爲ViewPager的頁面(因爲你已經定義了一個FragmentPagerAdapter實現entation)。

當你實例化FragmentPagerAdapter時,你給它一個FragmentManager。我相信你需要在頂層Fragment使用getChildFragmentManager(),例如像這樣:

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

這將提供特定於該特定片段和用於管理嵌套片段一FragmentManager實例。

相關問題