On the development of Android Viewpager

Source: Internet
Author: User

Before to write an Android native clock to use this viewpager, at that time on the Internet to find a lot of not an ideal tutorial, today simply to share their own ideas.

Viewpager and the ListView, we also need an adapter, he is pageradapter. Viewpager's function is to make the view slide, just as the lanucher slides around.

There are three steps to implement Viewpager:

1: Implement your own layout file.

2: Load the layout to be displayed.

3: Instantiate the Viewpager component, set Adapte, override Pageradapter.

Content_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:context= "Com.example.liberty.androidclick.MainActivity"Tools:showin= "@layout/activity_main"android:orientation= "vertical">    <Android.support.design.widget.TabLayoutAndroid:id= "@+id/tab"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/>        <Android.support.v4.view.ViewPagerAndroid:id= "@+id/viewpager"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/>    <!--This is the first step to add Android.support.v4.view.ViewPager to the layout file. -</Relativelayout>

Mainactivity.java

 PackageCom.example.liberty.androidclick;ImportAndroid.os.Bundle;Importandroid.support.design.widget.TabLayout;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsappcompatactivity {PrivateViewpager Mviewpager; PrivateTablayout mtablayout; PrivateLayoutinflater Minflater; Privatelist<string>mtitlelist=NewArraylist<>(); PrivateView view1,view2,view3,view4; Privatelist<view>mviewlist=NewArraylist<>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mviewpager=(Viewpager) Findviewbyid (R.id.viewpager); Mtablayout=(tablayout) Findviewbyid (R.id.tab); Minflater= Layoutinflater.from ( This); View1= Minflater.inflate (R.LAYOUT.LAYOUT1,NULL); View2= Minflater.inflate (R.layout.content_main,NULL); VIEW3= Minflater.inflate (R.layout.content_main,NULL); View4= Minflater.inflate (R.layout.content_main,NULL);        Mviewlist.add (View1);        Mviewlist.add (VIEW2);        Mviewlist.add (VIEW3); Mviewlist.add (VIEW4);//This is the second step of instantiating the Viewpager load to display the layoutMtitlelist.add ("No:1"); Mtitlelist.add ("No:2"); Mtitlelist.add ("No:3"); Mtitlelist.add ("No:4");        Mtablayout.settabmode (tablayout.mode_fixed); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (0))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (1))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (2))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (3))); Mypageradapter Madapter=NewMypageradapter (mviewlist); Mviewpager.setadapter (madapter);//Set MadapterMtablayout.setupwithviewpager (Mviewpager);    Mtablayout.settabsfrompageradapter (Madapter); }      classMypageradapterextendsPageradapter {PrivateList<view>mviewlist;  PublicMypageradapter (list<view>mviewlist) {             This. mviewlist =mviewlist; }//This is the third step to rewrite Pageradapter@Override Public intGetCount () {returnMviewlist.size ();//number of page cards} @Override PublicObject Instantiateitem (ViewGroup container,intposition) {Container.addview (Mviewlist.get (position));//Add a page card              returnMviewlist.get (position); } @Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {Container.removeview (Mviewlist.get (position));//Delete a page card} @Override Public Booleanisviewfromobject (View view, Object object) {return false; } @Override PublicCharsequence Getpagetitle (intposition) {              returnMtitlelist.get (position);//Page Card title          }    }}

As you can see from the code above, the Viewpager adapter is Pageradapter, which is the base class that provides the adapter to populate the page Viewpager inside. Google is thinking that Viewpager should be used with fragment.

And there is:

When you implement a pageradapter, you must overwrite at least the following methods:

    • instantiateItem(ViewGroup, int)
    • destroyItem(ViewGroup, int, Object)
    • getCount()
    • isViewFromObject(View, Object)

This is:

About Android Development Viewpager

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.