Android Learning--viewpage for multi-page sliding effects

Source: Internet
Author: User

The first step

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"  android:layout_width= "Match_parent"      android:layout_height= "Match_parent"   tools:context= ". Mainactivity ">  <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.PagerTitleStrip             android:id= "@+id/pagertitlestrip"              android:layoUt_width= "Wrap_content"             android:layout_ height= "Wrap_content"             android:layout_ gravity= "Top"             >           </android.support.v4.view.pagertitlestrip></ Android.support.v4.view.viewpager></relativelayout>

The second step is to create three tab pages with the simplest linear layout.


Part III

package xiaocool.net.viewpager2;import android.os.bundle;import  android.support.v4.view.pageradapter;import android.support.v4.view.pagertitlestrip;import  android.support.v4.view.viewpager;import android.support.v7.app.actionbaractivity;import  android.view.layoutinflater;import android.view.menu;import android.view.menuitem;import  android.view.view;import android.view.viewgroup;import java.util.arraylist;import  java.util.list;public class mainactivity extends actionbaractivity {     private ViewPager viewPager;    private PagerTitleStrip  pagertitlestrip;    private myadapter adapter=null;     private list<view> list=null; //loading the Loaded interface     private  list <string> title=null;//load the loaded interface corresponding to the title &NBSP;&NBSP;&NBSP;PRIVATE&NBSP;LAYOUTINFLATER&NBsp;inflater=null;//declares a dynamic layout object      @Override     protected void  oncreate (bundle savedinstancestate)  {         Super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);         viewpager= (ViewPager) this.findViewById (R.id.viewpager);         pagertitlestrip= (PagerTitleStrip) This.findviewbyid (R.id.pagertitlestrip);          adapter=new  myadapter ();         //Load Layout          inflater= layoutinflater.from (mainactivity.this);         //the layout object that was loaded for the first time         view tab=inflater.inflate ( R.layout.tab1,null);         lIst=new arraylist<view> ();         list.add (tab);         title=new ArrayList<String> ();         title.add ("title");         //first initialize page          viewpager.setadapter (Adapter);         viewpager.setonpagechangelistener (New viewpager.onpagechangelistener ()  {              @Override              public void onpagescrolled (int i, float v,  INT&NBSP;I2)  {            }              @Override              public&nbsP;void onpageselected (int i)  {             }            //when clicking on the current page, has not produced a skid is   The main completion of the interface loading             //here to open the thread to load the network data, update the UI operation              @Override              public void onpagescrollstatechanged (Int i)  {                view  view=inflater.inflate (r.layout.tab1,null);                 list.add (view);                 title.add ("New view");                 //Refresh Data                  adapter.notifydatasetchanged ();             }        });     }    // Layout Adapters     public class MyAdapter extends PagerAdapter{          @Override         public int  getcount ()  {            return  List.size ();         }       // Initialize adapters          @Override          public object instantiateitem (viewgroup container, int position)  {              ((Viewpager) container). AddView (List.get (position));             return list.get (position);         }       //from one layout to another, destroy the previous layout           @Override         public void  Destroyitem (Viewgroup container, int position, object object)  {             //super.destroyitem (Container, position,  object);             ((ViewPager) container). Removeview (List.get (position));        }         //get the layout of each interface            @Override          pUblic charsequence getpagetitle (int position)  {             return title.get (position);         }         @Override          Public boolean isviewfromobject (View view, object o)  {             return view==o;//determines whether the current view and object are the same object          }    }     @Override      public boolean oncreateoptionsmenu (Menu menu)  {         // Inflate the menu; this adds items to the  action bar if it is present.         Getmenuinflater (). Inflate (r.menu.menu_Main, menu);         return true;    }      @Override     public boolean onoptionsitemselected ( Menuitem item)  {        // handle action bar  item clicks here. The action bar will         // automatically handle clicks on the home/up button, so  long        // as you specify a parent  activity in androidmanifest.xml.        int id  = item.getitemid ();        //noinspection  simplifiableifstatement        if  (Id == R.id.action_ Settings)  {            return true;         }        return super.onoptionsitemselected (item);     }}

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5A/16/wKiom1T1tXuCwjt1AAICFJKliek732.jpg "style=" float: none; "title=" 11.png "alt=" Wkiom1t1txucwjt1aaicfjkliek732.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/12/wKioL1T1to-iEONJAAIIKoWs3eQ653.jpg "style=" float: none; "title=" Qq20150303212023.png "alt=" Wkiol1t1to-ieonjaaiikows3eq653.jpg "/>


This article is from the "Ahu-on-the-road" blog, be sure to keep this source http://xiaocool.blog.51cto.com/1593956/1617001

Android Learning--viewpage for multi-page sliding effects

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.