Viewpager multiple page swipe toggle

Source: Internet
Author: User

is what we need to achieve, the function has the following three:

1. Gesture swipe can switch the interface, click on the boot one to boot four can also switch to the corresponding screen.

2. When you switch the interface, the horizontal line below the guide will move to the corresponding place.

3. When there is no gesture, the interface will rotate automatically, there is a kind of advertising rotation effect.

In this article, we first implement the first function. To achieve this, we need to know the Viewpager control, which is a class of the android-support-v4.jar that comes with the Google SDK, and can be used for switching between screens. This package is added automatically when we create a new project, so there's no need to be nervous.

First, let's start by looking at how the main interface is designed:

Layout code is not posted out, in the middle of three red box, the first red box is four TextView, the second red box is a ImageView, the third red box is Viewpager. In the Viewpager, when switching the interface, is actually switching the view, so you need to create a corresponding four layout files, this layout file is very simple, only need to set the background as a picture.

Pageradaper's description

Like the ListView, we need an adapter, Viewpager the corresponding adapter is Pageradapter, this is an abstract class, so we need to write a class to inherit pageradapter. To inherit Pageradapter, at a minimum, you must override the following four methods:

1 Instantiateitem (ViewGroup, int.)
2. Destroyitem (viewgroup, int, Object)
3. GetCount ()
4. Isviewfromobject (View, Object)

The specific class can look at the following code:

 PackageCom.example.viewpager;Importjava.util.List;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.view.View; Public classMypageradaterextendsPageradapter {//list to place the page card interface    PrivateList<view>ListView;  PublicMypageradater (list<view>l) {ListView=l; }//when the image to be displayed can be cached, this method is called to initialize the display image, and we will add the ImageView to the ViewGroup and return it as the return value .@Override PublicObject Instantiateitem (View container,intposition) {        //TODO auto-generated Method Stub((Viewpager) container). AddView (Listview.get (position)); returnListview.get (position); }        //gets the number of controls to swipe, which should be the number of page cards shown here@Override Public intGetCount () {//TODO auto-generated Method Stub        returnlistview.size (); }        //to determine whether the same picture is displayed, here we compare two parameters to return@Override Public BooleanIsviewfromobject (View arg0, Object arg1) {//TODO auto-generated Method Stub        returnarg0 = =arg1; }        //Pageradapter only caches three images to be displayed, and if the sliding picture goes beyond the cached range, this method is called to destroy the image .@Override Public voidDestroyitem (View container,intposition, Object object) {        //TODO auto-generated Method Stub((Viewpager) container). Removeview (Listview.get (position)); }}

Use of Mypageradapter

Mypageradapter write well, how to use it, in fact, with the ListView is similar, in the Mypageradapter constructor we need to pass in the list array, the list of the page card interface, So you need to use Layoutinflater to get the view.

Layoutinflater inflater = getlayoutinflater (); Listview.add (inflater.inflate (R.LAYOUT.L1, null)); Listview.add (inflater.inflate (R.LAYOUT.L2, null)); Listview.add (inflater.inflate (R.LAYOUT.L3, null)); Listview.add (inflater.inflate (R.LAYOUT.L4, null));   

Then there is the simple Mviewpager.setadapter (Mpageradater), and mpageradater.notifydatasetchanged ();

Before we say click on the boot one to boot four can also switch to the corresponding screen, this is also very easy to implement, just set Texview listener and in the onclick function call Setcurrentitem (index).

The complete code will be given when the functionality is fully implemented.

  

Viewpager multiple page swipe toggle

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.