SlidingMenu與ViewPager雙向滑動的解決方案
阿新 • • 發佈:2019-02-19
最近研究SlidingMenu開源庫,發現它裡面有一個ViewPagerActivity的例子。但在這個例子中,ViewPager只支援單向滑動。稍作一點修改,即可實現雙向滑動。
- 原始的ViewPagerActivity.java程式碼。
- publicclass ViewPagerActivity extends BaseActivity {
- public ViewPagerActivity() {
- super(R.string.viewpager);
- }
- @Override
-
publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ViewPager vp = new ViewPager(this);
- vp.setId("VP".hashCode());
- vp.setAdapter(new ColorPagerAdapter(getSupportFragmentManager()));
- setContentView(vp);
-
vp.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- publicvoid onPageScrollStateChanged(int arg0) {
- }
- @Override
- publicvoid onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- publicvoid onPageSelected(int position) {
-
switch
- case0:
- getSlidingMenu().setTouchModeAbove(
- SlidingMenu.TOUCHMODE_FULLSCREEN);
- break;
- default:
- getSlidingMenu().setTouchModeAbove(
- SlidingMenu.TOUCHMODE_MARGIN);
- break;
- }
- }
- });
- vp.setCurrentItem(0);
- getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- }
- publicclass ColorPagerAdapter extends FragmentPagerAdapter {
- private ArrayList<Fragment> mFragments;
- privatefinalint[] COLORS = newint[] { R.color.red, R.color.green,
- R.color.blue, R.color.white, R.color.black };
- public ColorPagerAdapter(FragmentManager fm) {
- super(fm);
- mFragments = new ArrayList<Fragment>();
- for (int color : COLORS)
- mFragments.add(new ColorFragment(color));
- }
- @Override
- publicint getCount() {
- return mFragments.size();
- }
- @Override
- public Fragment getItem(int position) {
- return mFragments.get(position);
- }
- }
- }
- 改動後的ViewPagerActivity.java程式碼。
- publicclass ViewPagerActivity extends BaseActivity {
- public ViewPagerActivity() {
- super(R.string.viewpager);
- }
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
- getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
- getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame_two, new SampleListFragment()).commit();
- ViewPager vp = new ViewPager(this);
- vp.setId("VP".hashCode());
- final ColorPagerAdapter cpa = new ColorPagerAdapter(getSupportFragmentManager());
- vp.setAdapter(cpa);
- setContentView(vp);
- vp.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- publicvoid onPageScrollStateChanged(int arg0) {
- }
- @Override
- publicvoid onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- publicvoid onPageSelected(int position) {
- int lastPostion = cpa.getCount() - 1;
- //請看這裡
- if (position == 0) {
- getSlidingMenu().setMode(SlidingMenu.LEFT); // 第一個。SlidingMenu僅有左邊有效
- getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- } elseif (position == lastPostion) {
- getSlidingMenu().setMode(SlidingMenu.RIGHT); // 最後一個。SlidingMenu僅有右邊有效
- getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- } else {
- getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); // 中間,SlidingMenu無效
- }
- }
- });
- vp.setCurrentItem(0);
- getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- getSlidingMenu().setMode(SlidingMenu.LEFT);
- }
- publicclass ColorPagerAdapter extends FragmentPagerAdapter {
- private ArrayList<Fragment> mFragments;
- privatefinalint[] COLORS = newint[] { R.color.red, R.color.green, R.color.blue, R.color.white, R.color.black };
- public ColorPagerAdapter(FragmentManager fm) {
- super(fm);
- mFragments = new ArrayList<Fragment>();
- for (int color : COLORS)
- mFragments.add(new ColorFragment(color));
- }
- @Override
- publicint getCount() {
- return mFragments.size();
- }
- @Override
- public Fragment getItem(int position) {
- return mFragments.get(position);
- }
- }
- }