react-native-navigation的迁移库

ContentViewPagerAdapter.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.reactnativenavigation.screens;
  2. import android.support.v4.view.PagerAdapter;
  3. import android.support.v4.view.ViewPager;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import com.facebook.react.bridge.Arguments;
  7. import com.facebook.react.bridge.WritableMap;
  8. import com.reactnativenavigation.NavigationApplication;
  9. import com.reactnativenavigation.events.EventBus;
  10. import com.reactnativenavigation.events.ScreenChangedEvent;
  11. import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
  12. import com.reactnativenavigation.events.ViewPagerScreenScrollStartEvent;
  13. import com.reactnativenavigation.params.PageParams;
  14. import com.reactnativenavigation.views.ContentView;
  15. import java.util.List;
  16. class ContentViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
  17. private List<ContentView> contentViews;
  18. private List<PageParams> pageParams;
  19. private int currentPosition = 0;
  20. ContentViewPagerAdapter(List<ContentView> contentViews, List<PageParams> pageParams) {
  21. this.contentViews = contentViews;
  22. this.pageParams = pageParams;
  23. }
  24. @Override
  25. public Object instantiateItem(ViewGroup container, int position) {
  26. return contentViews.get(position);
  27. }
  28. @Override
  29. public int getCount() {
  30. return contentViews.size();
  31. }
  32. @Override
  33. public boolean isViewFromObject(View view, Object object) {
  34. return view == object;
  35. }
  36. @Override
  37. public CharSequence getPageTitle(int position) {
  38. return pageParams.get(position).title;
  39. }
  40. @Override
  41. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  42. }
  43. @Override
  44. public void onPageSelected(int position) {
  45. EventBus.instance.post(new ViewPagerScreenChangedEvent());
  46. currentPosition = position;
  47. EventBus.instance.post(new ScreenChangedEvent(pageParams.get(currentPosition)));
  48. sendTabSelectedEventToJs();
  49. }
  50. @Override
  51. public void onPageScrollStateChanged(int state) {
  52. if (state == ViewPager.SCROLL_STATE_DRAGGING) {
  53. EventBus.instance.post(new ViewPagerScreenScrollStartEvent());
  54. }
  55. }
  56. private void sendTabSelectedEventToJs() {
  57. WritableMap data = Arguments.createMap();
  58. String navigatorEventId = contentViews.get(currentPosition).getNavigatorEventId();
  59. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("tabSelected", navigatorEventId, data);
  60. }
  61. }