PagerTitleStrip under Android v4, page title of ViewPager
Android. support. v4.view. PagerTitleStrip
A custom View separated by the Title of the Page. This allows you to flexibly set the title style and text.
Effect:
Xml usage:
Android: layout_gravity: The title location is generally set to bottom or top.
I can see that this is used in the open-source project ImageLoader. I haven't seen any findbyid for a long time,
It turns out to be a child control of ViewPager in its source code.
Some source code:
@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); final ViewParent parent = getParent(); if (!(parent instanceof ViewPager)) { throw new IllegalStateException( PagerTitleStrip must be a direct child of a ViewPager.); } final ViewPager pager = (ViewPager) parent; final PagerAdapter adapter = pager.getAdapter(); pager.setInternalPageChangeListener(mPageListener); pager.setOnAdapterChangeListener(mPageListener); mPager = pager; updateAdapter(mWatchingAdapter != null ? mWatchingAdapter.get() : null, adapter); }
Analysis: When onAttachedToWindow () is used, locate the parent-view directly. If it is ViewPager, an exception is thrown.
Set title Value
In PagerAdapter, there is a getPageTitle (), which needs to be rewritten, and different titles can be returned based on different pages.
@Overridepublic CharSequence getPageTitle(int position) {switch (position) {case 0:return getString(R.string.title_list);case 1:return getString(R.string.title_grid);default:return null;}}
In the updateText () source code of PageTitleStrip, the adapter. getPageTitle of viewPager is called, and the title is obtained and set.