Take an example to explain the pagertitlestrip sub control in viewpager in Android

Let's take a look at a simple one. Let's start with an effect picture to attract everyone's attention.

The sliding between the three pages is sliding with the title above. Take a look at Android's official explanation of pagertitlestrip:

Pagertitlestrip is a non interactive indicator of viewpager about the current page, the previous page, and the next page. It is often added to the XML layout file as a child of the viewpager control. In your layout file, add it as a child control in viewpager. And its Android: Layout_ Set the gravity property to top or bottom to display it at the top or bottom of the viewpager. The title of each page is provided to viewpager through the getpagetitle (int) function of the adapter. Focus on two points:

1. First of all, the article mentioned: in your layout file, add it as a child control in viewpager.

2. Second, the title is obtained by rewriting the getpagetitle (int) function of the adapter.

Based on these two points, we can look at the code:

1. XML layout file:

It is clear that we embed. Pagertitlestrip directly as a child control of viewpager; This is the first step; Of course, Android: Layout_ The value of gravity = "" should be set to top or bottom. Displays the title block at the top or bottom. 2. Rewrite the getpagetitle() function of the adapter to facilitate an overall understanding. First post the global code, and then talk about it one by one:

3. Variable

A string array is applied to store the title 4 and initialization corresponding to the three pages

In the initialization phase, such a section of code for initializing the array is added. 5. Override the charsequence getpagetitle (int) function

Returns the current corresponding title according to the location.

As you can see, in fact, only the getpagetitle () function is rewritten here, and the above title bar function can be realized by returning different strings according to different positions. The first and second steps are about array and initialization. In fact, this is the first step. In fact, we can replace them with the following code:

This effect is the same, but the code is not easy to maintain.

Set title a custom view that separates the title of the page, so that you can flexibly set the style and text of the title.

effect:

XML usage:

android:layout_ Gravity controls the position of the title. Generally, the value is bottom or top

I saw that this was used in the open source project imageloader. I haven't seen any findbyid for a long time. It turned out that it was thought of as a child control of viewpager in its source code. Partial source code:

Analysis: when onattachedtowindow(), find the parent view directly. It can only be used if it is viewpager. Otherwise, throw an exception directly.

Set the Title Value: there is a getpagetitle () in pageradapter, which needs to be rewritten, and then different titles can be returned according to different pages.

In the updatetext () source code of pagetitlesstrip, you call the adapter.getpagetitle of viewpager, get the title and set it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>