Source:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout insertPoint = (RelativeLayout) findViewById(R.id.fragment_container); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.view_course_unit_pager, null); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); pager = (DisableableViewPager) findViewById(R.id.pager); pagerAdapter = new CourseUnitPagerAdapter(getSupportFragmentManager(), environment.getConfig(), unitList, courseData, this); pager.setAdapter(pagerAdapter); pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { private Boolean firstTime = true; @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (firstTime) { firstTime = false; CourseUnitFragment initialPage = (CourseUnitFragment) pagerAdapter.instantiateItem(pager, position); if (initialPage != null) { initialPage.onPageShow(); } } } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_DRAGGING) { int curIndex = pager.getCurrentItem(); PageViewStateCallback curView = (PageViewStateCallback) pagerAdapter.instantiateItem(pager, curIndex); if (curView != null) curView.onPageDisappear(); } if (state == ViewPager.SCROLL_STATE_IDLE) { int curIndex = pager.getCurrentItem(); PageViewStateCallback curView = (PageViewStateCallback) pagerAdapter.instantiateItem(pager, curIndex); if (curView != null) curView.onPageShow(); tryToUpdateForEndOfSequential(); } } }); findViewById(R.id.course_unit_nav_bar).setVisibility(View.VISIBLE); mPreviousBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { navigatePreviousComponent(); } }); mNextBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { navigateNextComponent(); } }); }

Target:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout insertPoint = (RelativeLayout) findViewById(R.id.fragment_container); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.view_course_unit_pager, null); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); pager = findViewById(R.id.pager); pagerAdapter = new CourseUnitPagerAdapter(getSupportFragmentManager(), environment.getConfig(), unitList, courseData, this); pager.setAdapter(pagerAdapter); pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { private boolean firstTime = true; @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (firstTime) { firstTime = false; final CourseUnitFragment initialPage = (CourseUnitFragment) pagerAdapter.instantiateItem(pager, position); if (initialPage != null) { initialPage.setUserVisibleHint(true); } } } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_IDLE) { tryToUpdateForEndOfSequential(); } } }); findViewById(R.id.course_unit_nav_bar).setVisibility(View.VISIBLE); mPreviousBtn.setOnClickListener(view -> navigatePreviousComponent()); mNextBtn.setOnClickListener(view -> navigateNextComponent()); }