Getting Started tutorial of viewpager component in android app development

First of all, let's have an overall understanding. Go directly to the last project and see that such powerful functions can be completed only through these lines of code. The next part will talk more about why it is written like this. Effect picture: realize the mutual sliding of three views. The first view slides to the second view:

Slide the second view to the third view:

1、 Create a new project and introduce the viewpager control viewpager. It is a class of an add-on package in Google SDK, which can be used to switch between screens.

1. Add in the main layout file

Where < Android. Support. V4. View. Viewpager / > is the component corresponding to viewpager, which should be placed where you want to slide

2. Create three new layouts. The views for sliding switching can also be seen from the effect drawing. Our three views are very simple. There are no controls in them. Of course, you can add various controls to them, but this is a demo, so I only use the background to distinguish them, not the layout.

The layout codes are as follows:

layout1.xml

layout2.xml

layout3.xml

2、 Code practice first on the overall code, and then explain it step by step.

The amount of code is very small, and it is all placed in the oncreate() function. 1. Let's first look at the meaning of the declared variable:

First, the viewpager corresponds to the < Android. Support. V4. View. Viewpager / > control.

View1 and view3 correspond to our three layouts, namely, layout1.xml, layout2.xml, and layout3.xml. Viewlist is a view array that holds the above three views

2. Next is their initialization process:

The initialization process is not difficult, that is, the resources and variables are linked to layout, and finally the instantiated view1 and view3 are added to the viewlist. 3. Pageadapter - the adapter of pageview, which must be unknown to everyone. There are adapters in listview. Listview obtains the item to be loaded by rewriting the getview() function. The pageadapter is different. After all, the pageadapter is a collection of individual views.

The pageadapter must override the following four functions:

(1) Boolean isviewfromobject (view arg0, object arg1) (2) int getcount() (3) void destroyitem (ViewGroup container, object object) (4) object instantiateitem (ViewGroup container, int position) let's take a look at the functions above:

Getcount(): returns the number of views to slide

Destroyitem(): deletes the view of the specified position from the current container;

Instantiateitem(): two things are done: first, add the current view to the container, and second, return the current view

Isviewfromobject(): we won't explain this function first. At present, we know that it needs to be rewritten in this way, and we will rewrite it later.

3、 For the understanding of key, viewpager does not directly deal with each view, but associates each view with a key. This key is used to track and uniquely represent a page. Moreover, this key is also independent of the location of the adapter where the page is located. When the pageadapter is about to change, it will call the startupdate function, and then call one or more instantiateitem or destroitem. Finally, finishupdate is called later in the update. When finishupdate returns, the object returned by instantiateitem should be added to the parent ViewGroup, and the object returned by destroyitem should be deleted by ViewGroup. Methodisviewfromobject (view, object) represents whether the current page is associated with a given key.

For a very simple pageadapter, you may choose to use the page itself as the key. After it is created and added to the ViewGroup, return the page itself in the instantiateitem method. Destroitem will remove the page from the ViewGroup. The isviewfromobject method can directly return view = = object. After the above explanation and practical examples, we must have a clear understanding of the concept of key. The following is an example to illustrate the relationship between key and view. Since key and view should correspond one by one, I take the position of each view as the key and change it on the basis of the examples in the previous chapter. Let's look at all the codes first, and then look at some explanations:

Two changes have been made here: 1. First, look at the location of the key, instantiateitem()

In this function, the key corresponds to the view currently loaded in the container as the return value. So here we return position and container. Addview (viewlist. Get (position)); The viewlist. Get (position) in the corresponds to this view. 2、isViewFromObject ()

Judge whether the key returned from instantiateitem () can correspond to the current view. We know that the key passed from instantiateitem is actually position, so we need to find the view according to position, and then judge with view arg0 in the parameter.

However, there is a problem in the real operation. We must first convert the object correspondence to the int type: (int) integer. ParseInt (arg1. Tostring()); Then find the corresponding view according to position;

Effect picture: sliding switching between three views

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