Android viewpager usage

Source: Internet
Author: User
Tags getcolor

This is a software package officially provided by Google that is compatible with Android devices of lower versions. It contains APIs that can only be used in Android 3.0 and later versions. Viewpager is one of them. We can do many things, from the simplest navigation to the page menu. So how can we use it? Like lisstview, we also need an adapter, Which is pageradapter. Take a look at the image of the API,

The viewpager function is to slide the view, just as sliding the Left and Right of lanucher. Use it in three steps:
1. Add it to the layout File
[HTML]
View plaincopy

  • <Android. Support. v4.view. viewpager: this component is used to display the sliding left and right interfaces. If the XML layout file is not loaded, the content is not displayed.
  • Android: Id = "@ + ID/viewpager"
  • Android: layout_width = "wrap_content"
  • Android: layout_height = "wrap_content"
  • Android: layout_gravity = "center">

2. Load the page card to display,[Java]

View plaincopy

  • Layoutinflater LF = getlayoutinflater (). From (this );
  • View1 = lf. Inflate (R. layout. layout1, null );
  • View2 = lf. Inflate (R. layout. layout2, null );
  • View3 = lf. Inflate (R. layout. layout3, null );
  • Viewlist = new arraylist (); // load the view to be displayed by page into the array.
  • Viewlist. Add (view1 );
  • Viewlist. Add (view2 );
  • Viewlist. Add (view3 );

3. instantiate the viewpager component in the activity and set its adapter (that is, pageradapter, with the same method as listview). Here, we generally need to override pageradapter.
[Java]
View plaincopy

  • Pageradapter = new pageradapter (){
  • @ Override
  • Public Boolean isviewfromobject (view arg0, object arg1 ){
  • Return arg0 = arg1;
  • }
  • @ Override
  • Public int getcount (){
  • Return viewlist. Size ();
  • }
  • @ Override
  • Public void destroyitem (viewgroup container, int position,
  • Object object ){
  • Container. removeview (viewlist. Get (position ));
  • }
  • @ Override
  • Public int getitemposition (Object object ){
  • Return super. getitemposition (object );
  • }
  • @ Override
  • Public charsequence getpagetitle (INT position ){
  • Return titlelist. Get (position );
  • }
  • @ Override
  • Public object instantiateitem (viewgroup container, int position ){
  • Container. addview (viewlist. Get (position ));
  • Weibo_button = (button) findviewbyid (R. Id. button1 );
  • Weibo_button.setonclicklistener (New onclicklistener (){
  • Public void onclick (view v ){
  • Intent = new intent (viewpagerdemo. This, weiboactivity. Class );
  • Startactivity (intent );
  • }
  • });
  • Return viewlist. Get (position );
  • }
  • };
  • Viewpager. setadapter (pageradapter );

This is a method for rewriting pageradapter. We can also do this:[Java]

View plaincopy

  • Public class myviewpageradapter extends pageradapter {
  • Private list mlistviews;
  • Public myviewpageradapter (list mlistviews ){
  • This. mlistviews = mlistviews; // constructor. The parameter is our page card, which is convenient.
  • }
  • @ Override
  • Public void destroyitem (viewgroup container, int position, object ){
  • Container. removeview (mlistviews. Get (position); // delete a page card
  • }
  • @ Override
  • Public object instantiateitem (viewgroup container, int position) {// This method is used to instantiate a page card
  • Container. addview (mlistviews. Get (position), 0); // Add a page card
  • Return mlistviews. Get (position );
  • }
  • @ Override
  • Public int getcount (){
  • Return mlistviews. Size (); // number of return page cards
  • }
  • @ Override
  • Public Boolean isviewfromobject (view arg0, object arg1 ){
  • Return arg0 = arg1; // The official prompt is as follows:
  • }
  • }

The similarities and differences between them must be very important, that is, the methods we need to rewrite. From the above picture, we can see that the viewpager adapter is pageradapter, which provides an adapter for the base class to fill in the inside of the page viewpager. You may want to use a more specific implementation, such as fragmentpageradapter or fragmentstatepageradapter. It should be noted that viewpager should be used with fragment, at least Google thinks so officially, but below 3.0, we do not need to do so. Note that when you implement a pageradapter, you must cover at least the following methods:

  • Instantiateitem (viewgroup, INT)
  • Destroyitem (viewgroup, Int, object)
  • Getcount ()
  • Isviewfromobject (view, object)

From the above example, we can see that we have implemented at least the above four methods. Of course, if you want to make the program more robust or more comprehensive, you can rewrite other methods. The first complete sample code is as follows:

Home Page activity:
[Java]
View plaincopy

  • Package com. example. viewpagerdemo;
  • Import java. util. arraylist;
  • Import java. util. List;
  • Import Android. OS. Bundle;
  • Import Android. App. activity;
  • Import Android. content. context;
  • Import Android. content. intent;
  • Import Android. Support. v4.view. pageradapter;
  • Import Android. Support. v4.view. pagertabstrip;
  • Import Android. Support. v4.view. pagertitlestrip;
  • Import Android. Support. v4.view. viewpager;
  • Import Android. util. attributeset;
  • Import Android. View. layoutinflater;
  • Import Android. View. Menu;
  • Import Android. View. view;
  • Import Android. View. View. onclicklistener;
  • Import Android. View. viewgroup;
  • Import Android. widget. Button;
  • Public class viewpagerdemo extends activity {
  • Private view view1, view2, view3; // The page card to slide
  • Private viewpager; // viewpager
  • Private pagertitlestrip; // the title of viewpager
  • Private pagertabstrip; // A viewpager indicator with a thick horizontal underline
  • Private list viewlist; // Add the page card to be slide to this list
  • Private list titlelist; // the title of viewpager
  • Private button weibo_button; // button object, which will be used to enter the second viewpager example later
  • Private intent;
  • @ Override
  • Public void oncreate (bundle savedinstancestate ){
  • Super. oncreate (savedinstancestate );
  • Setcontentview (R. layout. activity_view_pager_demo );
  • Initview ();
  • }
  • /* Here we need to explain that we have seen pagertabstrip and pagertitlestrip in the above image. They are actually a indicator of viewpager. The former effect is a thick underline, the latter is used to display the title of each page card. Of course, this can also coexist. When using them, you must note that you should add the following layout file in Android. Support. v4.view. viewpager.
  • Android. Support. v4.view. pagertabstrip and Android. Support. v4.view. pagertitlestrip.
  • Private void initview (){
  • Viewpager = (viewpager) findviewbyid (R. Id. viewpager );
  • // Pagertitlestrip = (pagertitlestrip) findviewbyid (R. Id. pagertitle );
  • Pagertabstrip = (pagertabstrip) findviewbyid (R. Id. pagertab );
  • Pagertabstrip. settabindicatorcolor (getresources (). getcolor (R. color. Gold ));
  • Pagertabstrip. setdrawfullunderline (false );
  • Pagertabstrip. setbackgroundcolor (getresources (). getcolor (R. color. Azure ));
  • Pagertabstrip. settextspacing (50 );
  • /*
  • Weibo_button = (button) findviewbyid (R. Id. button1 );
  • Weibo_button.setonclicklistener (New onclicklistener (){
  • Public void onclick (view v ){
  • Intent = new intent (viewpagerdemo. This, weiboactivity. Class );
  • Startactivity (intent );
  • }
  • });
  • */
  • View1 = findviewbyid (R. layout. layout1 );
  • View2 = findviewbyid (R. layout. layout2 );
  • View3 = findviewbyid (R. layout. layout3 );
  • Layoutinflater LF = getlayoutinflater (). From (this );
  • View1 = lf. Inflate (R. layout. layout1, null );
  • View2 = lf. Inflate (R. layout. layout2, null );
  • View3 = lf. Inflate (R. layout. layout3, null );
  • Viewlist = new arraylist (); // load the view to be displayed by page into the array.
  • Viewlist. Add (view1 );
  • Viewlist. Add (view2 );
  • Viewlist. Add (view3 );
  • Titlelist = new arraylist (); // The title data of each page
  • Titlelist. Add ("WP ");
  • Titlelist. Add ("JY ");
  • Titlelist. Add ("updated ");
  • Pageradapter = new pageradapter (){
  • @ Override
  • Public Boolean isviewfromobject (view arg0, object arg1 ){
  • Return arg0 = arg1;
  • }
  • @ Override
  • Public int getcount (){
  • Return viewlist. Size ();
  • }
  • @ Override
  • Public void destroyitem (viewgroup container, int position,
  • Object object ){
  • Container. removeview (viewlist. Get (position ));
  • }
  • @ Override
  • Public int getitemposition (Object object ){
  • Return super. getitemposition (object );
  • }
  • @ Override
  • Public charsequence getpagetitle (INT position ){
  • Return titlelist. Get (position); // directly use an adapter to display the title. Therefore, we can see from the above that pagertitlestrip is not used. Of course you can use it.
  • }
  • @ Override
  • Public object instantiateitem (viewgroup container, int position ){
  • Container. addview (viewlist. Get (position ));
  • Weibo_button = (button) findviewbyid (R. id. button1); // note that we instantiate the button component in the rewrite Adapter. If you do this in the oncreate () method, an error is returned.
  • Weibo_button.setonclicklistener (New onclicklistener (){
  • Public void onclick (view v ){
  • Intent = new intent (viewpagerdemo. This, weiboactivity. Class );
  • Startactivity (intent );
  • }
  • });
  • Return viewlist. Get (position );
  • }
  • };
  • Viewpager. setadapter (pageradapter );
  • }
  • @ Override
  • Public Boolean oncreateoptionsmenu (menu ){
  • Getmenuinflater (). Inflate (R. Menu. activity_view_pager_demo, menu );
  • Return true;
  • }
  • }

Its Layout file:
[Java]
View plaincopy

  • Android: layout_width = "fill_parent"
  • Android: layout_height = "fill_parent"
  • Androidrientation = "vertical">
  • <Android. Support. v4.view. viewpager
  • Android: Id = "@ + ID/viewpager"
  • Android: layout_width = "wrap_content"
  • Android: layout_height = "wrap_content"
  • Android: layout_gravity = "center">
  • <Android. Support. v4.view. pagertabstrip = ""
  • Android: Id = "@ + ID/pagertab"
  • Android: layout_width = "wrap_content"
  • Android: layout_height = "wrap_content"
  • Android: layout_gravity = "TOP"/>
  • Lt; linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "fill_parent"
  • Androidrientation = "vertical">
  • <Linearlayout
  • Android: Id = "@ + ID/linearlayout1"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "401_dip"
  • Android: Background = "# ffffff">
  • <Textview
  • Android: Id = "@ + ID/text1"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "fill_parent"
  • Android: layout_weight = "1.0"
  • Android: gravity = "center"
  • Android: text = "@ me"
  • Android: textcolor = "#000000"
  • Android: textsize = "2020.dip"/>
  • <Textview
  • Android: Id = "@ + ID/text2"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "fill_parent"
  • Android: layout_weight = "1.0"
  • Android: gravity = "center"
  • Android: text = "comment"
  • Android: textcolor = "#000000"
  • Android: textsize = "2020.dip"/>
  • <Textview
  • Android: Id = "@ + ID/text3"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "fill_parent"
  • Android: layout_weight = "1.0"
  • Android: gravity = "center"
  • Android: text = "private message"
  • Android: textcolor = "#000000"
  • Android: textsize = "2020.dip"/>
  • <Imageview
  • Android: Id = "@ + ID/cursor"
  • Android: layout_width = "fill_parent"
  • Android: layout_height = "wrap_content"
  • Android: scaletype = "matrix"
  • Android: src = "@ drawable/a"/>
  • <Android. Support. v4.view. viewpager
  • Android: Id = "@ + ID/vpager"
  • Android: layout_width = "wrap_content"
  • Android: layout_height = "0dp"
  • Android: layout_gravity = "center"
  • Android: layout_weight = "1.0"
  • Android: Background = "#000000"
  • Android: flipinterval = "30"
  • Android: persistentdrawingcache = "Animation"/>
  • Lt;/linearlayout>

The effect is as follows:


So to sum up, what can we do with viewpager:
1. The program uses navigation and the effect of the dot at the bottom, which is described in the example.
2. Slide the page and add the menu effect. You can click the icon at the bottom of the page, or click Weibo today, whether it is a gesture.
Switch from csdn

Related Article

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.