Viewpager is used in Android to realize screen page switching and page rotation effect

About how to realize screen page switching, I wrote a blog post "using viewflipper to realize screen switching in Android". Compared with viewflipper, viewpager is more suitable for complex view switching, and viewpager has its own adapter, which also allows it to adapt to complex objects and realize dynamic loading of data.

Viewpager is a software package compatible with low version Android devices officially provided by Google, which contains APIs that can only be used in Android 3.0 or above. Viewpager is one of them. With it, we can do many things, from the simplest navigation to page menu and so on.

Here are two simple effects that viewpager can achieve:

The first is the switching of screen pages (similar to the implementation effect of viewflipper)

The implementation effect is as follows (4 views (layout)):

Step 1: add the main layout file viewpager to the layout file_ layout.xml

matters needing attention:

< 1. Here, both viewpager and pagertabstrip should write the package name completely, otherwise classnotfound will occur

< 2. The API says that pagertabstrip is used as a sub tag of viewpager in the layout XML. It cannot be taken out, otherwise an error will be reported

< 3. The property Android: layout can be used in the pagertabstrip tag_ Gravity = top | bottom to specify the location of the title

< 4. If you want to display the title of a page of pagertabstrip, you need to implement getpagetitle (int) in the adapter of viewpager

Step 2: create a view file to show the switch in the layout (there are four layout1 / 2 / 3 / 4.xml in the example, and a typical one is written here):

Step 3: the implementation code of activity in Java is viewpagerdemo.java (the property of pagertabstrip is not set here):

Instantiate the viewpager component in the activity and set its adapter (that is, pageradapter, with the same method as listview)

Implement a pageradapter that overrides the following methods:

Instantiateitem (ViewGroup, int) / / used to instantiate page cards

Destroyitem (ViewGroup, int, object) / / delete the page card

Getcount() / / returns the number of page cards

Isviewfromobject (view, object) / / judge whether two objects are equal

Getpagetitle (int position) / / set the title displayed by the tag

Sets the properties of the indicator label

pagerTabStrip.setTabIndicatorColor();// Color of indicator

pagerTabStrip.setBackgroundColor();// Back color

pagerTabStrip.setTextColor(Color.WHITE);// Font color

The second: page rotation effect view (implemented on the boot page when the program is started for the first time)

The implementation effect is as follows (sliding guidance of 3 views):

Comments before starting code:

1. This round robin effect is not realized; 2. Navigation origin resource picture annotation: default_ Holo is unchecked and touched_ Holo is filled when selected. (you can also paint with shape)

Step 1: add the main layout file viewpager to the layout file_ layout.xml

The main layout is FrameLayout, and a LinearLayout layout containing three navigation origins (determined by the number of sliding views) is nested on the viewpager (pagertabstrip is not set because there are navigation origins here) (this layout is at the bottom this time):

Step 2: the view used for sliding switching in layout (there are three layout1 / 2 / 3.xml in the example, and a typical one is written here) is the same as the first one

Step 3: the implementation code of activity in Java mainactivity.java

The change of the navigation origin state during view switching is realized by implementing the onpagechangelistener interface. For a detailed explanation of the methods in onpagechangelistener, please refer to the blog post

Detailed explanation of setonpagechangelistener method of viewpager will not be repeated here:

So far, the code required for the implementation example of viewpager has been completed. Of course, this is only the simplest function of viewpager. Later, some advanced usage of viewpager will be updated. Welcome to continue to support.

The above is what Xiaobian introduced to you. In Android, viewpager is used to achieve screen page switching and page rotation effect. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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