PagerTitleStrip under Android v4, page title of ViewPager, androidviewpager

Source: Internet
Author: User

PagerTitleStrip under Android v4, page title of ViewPager, androidviewpager

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.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/pager"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.PagerTitleStripandroid:id="@+id/pager_title_strip"android:layout_width="match_parent"android:layout_height="30dp"android:layout_gravity="bottom"android:paddingTop="4dp"android:paddingBottom="4dp" /></android.support.v4.view.ViewPager>

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.