react-native-navigation的迁移库

ViewPagerScreen.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.reactnativenavigation.screens;
  2. import android.content.Context;
  3. import android.support.design.widget.TabLayout;
  4. import android.support.v4.view.ViewPager;
  5. import android.support.v7.app.AppCompatActivity;
  6. import com.reactnativenavigation.events.Event;
  7. import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
  8. import com.reactnativenavigation.params.BaseScreenParams;
  9. import com.reactnativenavigation.params.PageParams;
  10. import com.reactnativenavigation.params.ScreenParams;
  11. import com.reactnativenavigation.views.ContentView;
  12. import com.reactnativenavigation.views.LeftButtonOnClickListener;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  16. public class ViewPagerScreen extends Screen {
  17. private static final int OFFSCREEN_PAGE_LIMIT = 99;
  18. protected List<ContentView> contentViews;
  19. protected ViewPager viewPager;
  20. public ViewPagerScreen(AppCompatActivity activity, ScreenParams screenParams, LeftButtonOnClickListener backButtonListener) {
  21. super(activity, screenParams, backButtonListener);
  22. }
  23. @Override
  24. public BaseScreenParams getScreenParams() {
  25. return screenParams.topTabParams.get(viewPager.getCurrentItem());
  26. }
  27. @Override
  28. protected void createContent() {
  29. TabLayout tabLayout = topBar.initTabs();
  30. createViewPager();
  31. addPages();
  32. setupViewPager(tabLayout);
  33. }
  34. private void createViewPager() {
  35. viewPager = createViewPager(getContext());
  36. viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
  37. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  38. if (screenParams.styleParams.drawScreenBelowTopBar) {
  39. lp.addRule(BELOW, topBar.getId());
  40. }
  41. addView(viewPager, lp);
  42. }
  43. protected ViewPager createViewPager(Context context) {
  44. return new ViewPager(context);
  45. }
  46. private void addPages() {
  47. contentViews = new ArrayList<>();
  48. for (PageParams tab : screenParams.topTabParams) {
  49. addPage(tab);
  50. }
  51. }
  52. private void addPage(PageParams tab) {
  53. ContentView contentView = createContentView(tab);
  54. addContent(contentView);
  55. contentViews.add(contentView);
  56. }
  57. protected ContentView createContentView(PageParams tab) {
  58. return new ContentView(getContext(), tab.screenId, tab.navigationParams);
  59. }
  60. private void setupViewPager(TabLayout tabLayout) {
  61. ContentViewPagerAdapter adapter = new ContentViewPagerAdapter(contentViews, screenParams.topTabParams);
  62. viewPager.setAdapter(adapter);
  63. viewPager.addOnPageChangeListener(adapter);
  64. tabLayout.setupWithViewPager(viewPager);
  65. }
  66. private void addContent(ContentView contentView) {
  67. LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  68. viewPager.addView(contentView, params);
  69. }
  70. @Override
  71. public void unmountReactView() {
  72. for (ContentView contentView : contentViews) {
  73. contentView.unmountReactView();
  74. }
  75. }
  76. @Override
  77. public void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener) {
  78. contentViews.get(0).setOnDisplayListener(onContentViewDisplayedListener);
  79. }
  80. @Override
  81. public String getScreenInstanceId() {
  82. return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.screenInstanceId;
  83. }
  84. @Override
  85. public String getNavigatorEventId() {
  86. return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.navigatorEventId;
  87. }
  88. }