react-native-navigation的迁移库

ViewPagerScreen.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.reactnativenavigation.screens;
  2. import android.support.design.widget.TabLayout;
  3. import android.support.v4.view.ViewPager;
  4. import android.support.v7.app.AppCompatActivity;
  5. import com.reactnativenavigation.params.ScreenParams;
  6. import com.reactnativenavigation.params.TopTabParams;
  7. import com.reactnativenavigation.views.ContentView;
  8. import com.reactnativenavigation.views.TitleBarBackButtonListener;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  12. public class ViewPagerScreen extends Screen {
  13. private static final int OFFSCREEN_PAGE_LIMIT = 99;
  14. private List<ContentView> contentViews;
  15. private ViewPager viewPager;
  16. public ViewPagerScreen(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener backButtonListener) {
  17. super(activity, screenParams, backButtonListener);
  18. }
  19. @Override
  20. protected void createContent() {
  21. TabLayout tabLayout = topBar.initTabs();
  22. createViewPager();
  23. addPages();
  24. setupViewPager(tabLayout);
  25. }
  26. private void createViewPager() {
  27. viewPager = new ViewPager(getContext());
  28. viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
  29. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  30. if (screenParams.styleParams.drawScreenBelowTopBar) {
  31. lp.addRule(BELOW, topBar.getId());
  32. }
  33. addView(viewPager, lp);
  34. }
  35. private void addPages() {
  36. contentViews = new ArrayList<>();
  37. for (TopTabParams tab : screenParams.topTabParams) {
  38. ContentView contentView = new ContentView(getContext(), screenParams, tab.screenId);
  39. addContent(contentView);
  40. contentViews.add(contentView);
  41. }
  42. }
  43. private void setupViewPager(TabLayout tabLayout) {
  44. ContentViewPagerAdapter adapter = new ContentViewPagerAdapter(contentViews, screenParams.topTabParams);
  45. viewPager.setAdapter(adapter);
  46. tabLayout.setupWithViewPager(viewPager);
  47. }
  48. private void addContent(ContentView contentView) {
  49. LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  50. viewPager.addView(contentView, params);
  51. }
  52. @Override
  53. public void ensureUnmountOnDetachedFromWindow() {
  54. for (ContentView contentView : contentViews) {
  55. contentView.ensureUnmountOnDetachedFromWindow();
  56. }
  57. }
  58. @Override
  59. public void preventUnmountOnDetachedFromWindow() {
  60. for (ContentView contentView : contentViews) {
  61. contentView.preventUnmountOnDetachedFromWindow();
  62. }
  63. }
  64. @Override
  65. public void preventMountAfterReattachedToWindow() {
  66. for (ContentView contentView : contentViews) {
  67. contentView.preventMountAfterReattachedToWindow();
  68. }
  69. }
  70. }