private void initViewPagerAndTabs() { ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); pagerAdapter.addFragment(PartThreeFragment.createInstance(20), "One"); pagerAdapter.addFragment(PartThreeFragment.createInstance(4), "Two"); viewPager.setAdapter(pagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager); }
void updateAdapter(PagerAdapter oldAdapter, PagerAdapter newAdapter) { if (oldAdapter != null) { oldAdapter.unregisterDataSetObserver(mPageListener); } if (newAdapter != null) { newAdapter.registerDataSetObserver(mPageListener); } if (mPager != null) { mLastKnownCurrentPage = -1; mLastKnownPositionOffset = -1; updateText(mPager.getCurrentItem(), newAdapter); requestLayout(); } }
/** * Adds a new slide * * @param fragment Instance of Fragment which should be added as slide */ public void addSlide(@NonNull Fragment fragment) { fragments.add(fragment); if (isWizardMode) { setOffScreenPageLimit(fragments.size()); } mPagerAdapter.notifyDataSetChanged(); }
/** * Called before a slide change happens. By returning false, one can disallow the slide change. * * @return true, if the slide change should be allowed, else false */ private boolean handleBeforeSlideChanged() { Fragment currentFragment = mPagerAdapter.getItem(pager.getCurrentItem()); LogHelper.d( TAG, String.format( "User wants to move away from slide: %s. Checking if this should be allowed...", currentFragment)); // Check if the current fragment implements ISlidePolicy, else a change is always allowed if (currentFragment instanceof ISlidePolicy) { ISlidePolicy slide = (ISlidePolicy) currentFragment; LogHelper.d(TAG, "Current fragment implements ISlidePolicy."); // Check if policy is fulfilled if (!slide.isPolicyRespected()) { LogHelper.d(TAG, "Slide policy not respected, denying change request."); return false; } } LogHelper.d(TAG, "Change request will be allowed."); return true; }
private void handleIllegalSlideChangeAttempt() { Fragment currentFragment = mPagerAdapter.getItem(pager.getCurrentItem()); if (currentFragment != null && currentFragment instanceof ISlidePolicy) { ISlidePolicy slide = (ISlidePolicy) currentFragment; if (!slide.isPolicyRespected()) { slide.onUserIllegallyRequestedNextPage(); } } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); viewPager = (ViewPager) findViewById(R.id.pager); PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), this); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); for (int i = 0; i < adapter.getCount(); i++) { actionBar.addTab(actionBar.newTab().setText(adapter.getPageTitle(i)).setTabListener(this)); } }
void updateText(int currentItem, PagerAdapter adapter) { final int itemCount = adapter != null ? adapter.getCount() : 0; mUpdatingText = true; CharSequence text = null; if (currentItem >= 1 && adapter != null) { text = adapter.getPageTitle(currentItem - 1); } mPrevText.setText(text); mCurrText.setText(adapter != null ? adapter.getPageTitle(currentItem) : null); text = null; if (currentItem + 1 < itemCount && adapter != null) { text = adapter.getPageTitle(currentItem + 1); } mNextText.setText(text); // Measure everything final int width = getWidth() - getPaddingLeft() - getPaddingRight(); final int childHeight = getHeight() - getPaddingTop() - getPaddingBottom(); final int childWidthSpec = MeasureSpec.makeMeasureSpec((int) (width * 0.8f), MeasureSpec.AT_MOST); final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY); mPrevText.measure(childWidthSpec, childHeightSpec); mCurrText.measure(childWidthSpec, childHeightSpec); mNextText.measure(childWidthSpec, childHeightSpec); mLastKnownCurrentPage = currentItem; if (!mUpdatingPositions) { updateTextPositions(currentItem, mLastKnownPositionOffset); } mUpdatingText = false; }
@NonNull public List<MyFragment> getSlides() { return mPagerAdapter.getFragments(); }
public void addSlide(@NonNull MyFragment fragment) { fragments.add(fragment); mPagerAdapter.notifyDataSetChanged(); }
public void addSlide(@NonNull Fragment fragment, @NonNull Context context) { fragments.add(Fragment.instantiate(context, fragment.getClass().getName())); mPagerAdapter.notifyDataSetChanged(); }
private void initFragment() { ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); pagerAdapter.addFragment(DetailsFragment.createInstance(_deposit.get_id()), "Details"); viewPager.setAdapter(pagerAdapter); }