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; }
@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)); } }