Explain the usage of pagertabstrip sub control of viewpager in Android

Let's start with a small example:

It can be seen that the effect is also great, which is more fluent than the previously customized label indicator. Next, briefly introduce pagertabstrip and its use.

Pagertabstrip is a class in the V4 support package. It is a special class for viewpager and cannot be used elsewhere. When using, we only need to declare it in the layout of viewpager.

As shown in the following code

We can set the layout_ Gravity attribute, such as top or bottom, controls the display position. In addition, no other settings are required.

The rest is what we need to implement in the code.

In the above code, we show how to control the text of the title. We just need to rewrite the getpagertitle method. Let's talk about pagertabstrip in detail with an example:

Pagertabstrip is an 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. The usage is exactly the same as pagertitlestrip, that is:

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.

Take a look at the examples: 1. XML layout

You can see that, similarly, pagertabstrip is directly inserted as a child control of viewpager. Of course, Android: layout_ The value of gravity = "" should be set to top or bottom. 2. Rewrite all codes of getpagetitle() function of adapter:

The code here is exactly the same as that of pagertitlestrip, so I won't explain it any more. In this way, we have finished the simple use of pagertabstrip. Let's talk about the extension of pagertabstrip.

3. Extension: the pagertabstrip property is changed. In the source code, you can see a project called testviewpage_ PagerTabStrip_ Extension, run it. The effect is as follows:

As can be seen from the above two figures, I changed two places:

(1) Underline color, the original is black, I became green;

(2) Add a picture before the tab title;

Here's how to change it:

1. Change the underline color: it mainly depends on the settabindirectorcolorresource method of pagertabstrip;

The code is as follows:

2. Add title -- rewrite the adapter charsequence getpagetitle (int) method. In the charsequence getpagetitle (int position) method, the return value is that we do not return a string object, but use spannablestringbuilder to construct an extended string object containing pictures; The specific code is as follows. We won't talk about it in detail. You can see the usage of spannablestringbuilder.

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
分享
二维码
< <上一篇
下一篇>>