我有活动和滑块菜单。单击滑块菜单我正在替换活动上的许多其他片段。但在第一个位置我有查看寻呼机。所以一旦我要点击第二个位置,其他片段正在替换,但是一旦我点击第一个位置,viewpager就没有创建。
public class MainActivity extends SlidingActivity implements FragmentDrawer.FragmentDrawerListener, CaseStudiesFragment.OnCaseStudiesAttachedFragmentListener {
private String toolbarTitle;
private ViewPager pager;
SwipeMainFragmentPagerAdapter swipeMainFragmentPagerAdapter;
private Toolbar toolbar;
private List<Integer> _pageHistory;
Fragment fragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.menu_frame);
_pageHistory = new ArrayList<>();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_menu);
FragmentDrawer drawerMenuFragment = new FragmentDrawer();
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.menu_frame, drawerMenuFragment);
t.commit();
SlidingMenu sm = getSlidingMenu();
sm.setOnClosedListener(new SlidingMenu.OnClosedListener() {
@Override
public void onClosed() {
}
});
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setFadeDegree(0.35f);
sm.setMode(SlidingMenu.LEFT);
sm.setBehindWidthRes(R.dimen.slidingmenu_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setShadowDrawable(R.drawable.shadow);
setUpViewPager();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", fragment);
}
private void setUpViewPager() {
pager = (ViewPager) findViewById(R.id.viewpager);
swipeMainFragmentPagerAdapter = new SwipeMainFragmentPagerAdapter(getSupportFragmentManager());
pager.setAdapter(swipeMainFragmentPagerAdapter);
pager.getAdapter().notifyDataSetChanged();
// pager.invalidate();
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
break;
default:
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
break;
}
}
});
pager.setCurrentItem(0);
}
@Override
public void onCaseStudy(View view, int position) {
}
private class SwipeMainFragmentPagerAdapter extends FragmentStatePagerAdapter {
public SwipeMainFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
final Fragment result;
switch (position) {
case 0:
result = new SwipeCardViewFragment();
break;
case 1:
result = new WebViewsFragment();
break;
default:
result = null;
break;
}
return result;
}
@Override
public int getCount() {
return 2;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
return fragment;
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
getSlidingMenu().showContent();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
toggle();
}
return super.onOptionsItemSelected(item);
}
private void displayView(int position) {
toolbarTitle = getString(R.string.all_updates_item);
switch (position) {
case 0:
//
if (pager.getCurrentItem()!=position){
pager.setCurrentItem(0);
}
toolbarTitle = getString(R.string.all_updates_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 1:
pager.setCurrentItem(0);
toolbarTitle = getString(R.string.trending_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 2:
pager.setCurrentItem(0);
toolbarTitle = getString(R.string.notification_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 3:
pager.setCurrentItem(0);
toolbarTitle = getString(R.string.bookmarks_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 4:
pager.setCurrentItem(0);
toolbarTitle = getString(R.string.medical_updates_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 5:
fragment = new CaseStudiesFragment();
toolbarTitle = getString(R.string.case_studies_item);
break;
case 6:
fragment = new ProductCatelogFragment();
toolbarTitle = getString(R.string.product_catelog);
break;
case 7:
pager.setCurrentItem(0);
toolbarTitle = getString(R.string.webinars_item);
getSupportActionBar().setTitle(toolbarTitle);
break;
case 8:
fragment = new PollFragment();
toolbarTitle = getString(R.string.poll_item);
break;
case 9:
fragment = new ConnectionsFragment();
toolbarTitle = getString(R.string.connection_item);
break;
case 10:
fragment = new ReportAnAeFragment();
toolbarTitle = getString(R.string.report_item);
break;
case 12:
fragment = new MedicalInfoFragment();
toolbarTitle = getString(R.string.medical_info_item);
break;
case 13:
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(toolbarTitle);
fragmentTransaction.commit();
// getSupportActionBar().setTitle(null);
}
}
}