The use summary of Android Viewpager _android

Source: Internet
Author: User
Tags object object stub

In a window to add a tab note, complete the note switch to achieve the page switch, the advantage is that you can have multiple pages in the same window, these pages share the same window of resources, with multiple windows to achieve this feature more fluent!!

The main resulting class files have activity,n view,adapter, custom viewpager,n+1 layout files

The file used for the demo

Steps:

Create an activity

Package Com.example.myviewpager;
Import java.util.ArrayList;

Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.support.v4.view.ViewPager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.TextView;  /** * Main Window * @author CGX * * * * * * * * * * */public class Mainactivity extends active implements {private context
 Mcontext;
 Private Myviewpager mpager;//page content private Myviewpageradapter pageradapter = null;
 Private TextView T1, T2, t3;//page card header private list<view> pagelist = new arraylist<view> ();
 Private View1 MView1;
 Private View2 mView2;

 Private VIEW3 mView3;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Mcontext = this;
 Initviews ();
 Initevents ();
 Initviewpager (); private void Initviews () {//TODO auto-generateD method Stub t1 = (TextView) Findviewbyid (R.ID.TEXT1);
 T2 = (TextView) Findviewbyid (R.ID.TEXT2);
 T3 = (TextView) Findviewbyid (R.ID.TEXT3);
 Mpager = (Myviewpager) Findviewbyid (R.id.vpager);
 Setting Viewpager does not allow sliding//mpager.setcanscroll (false);
 At the beginning of the window, the default first note is selected T1.setbackgroundcolor (Color.parsecolor ("#FFFF00"));
 T2.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));
 T3.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));
 private void Initevents () {//TODO auto-generated Method stub T1.setonclicklistener (this);
 T2.setonclicklistener (this);
 T3.setonclicklistener (this);

 private void Initviewpager () {//TODO auto-generated Method Stub pagelist.clear ();
 if (MView1 = = null) {MView1 = new View1 (mcontext);
 } if (mView2 = = null) {mView2 = new View2 (mcontext);
 } if (mView3 = = null) {MVIEW3 = new View3 (mcontext);
 } pagelist.add (Mview1.getview ());
 Pagelist.add (Mview2.getview ());

 Pagelist.add (Mview3.getview ()); Pageradapter = new Myviewpageradapter (PageList);
 Cache page, if you want to all cache, the parameter equals page card number minus one, the system default parameter is 1, save two mpager.setoffscreenpagelimit (2);
 Mpager.setadapter (Pageradapter);
 Set Page Change Listener mpager.setonpagechangelistener (Onpagechangelistener); /** * Simpleonpagechangelistener. This listening is triggered when our Viewpager page is switched and we will change the tab focus.
 Because the implementation of Viewpager and Actionbar is independent, we need to manually synchronize.
 * * Viewpager.simpleonpagechangelistener Onpagechangelistener = new Viewpager.simpleonpagechangelistener () {@Override
  The public void onpageselected (int position) {/** * Setselectednavigationitem method is used to set the Focus tab for Actionbar.
  * In the next we judge the Slidingmenu mode of hand power, if the Viewpager has slipped to the left, then we set the gesture to the screen, * so that when we slide to the left, it will open the menu.
 * * INITTAB (position); }//Initialize the note color private void initTab (int position) {//TODO auto-generated method stub if (position = = 0) {T1.SETB
  Ackgroundcolor (Color.parsecolor ("#FFFF00"));
  T2.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));

  T3.setbackgroundcolor (Color.parsecolor ("#FFFFFF")); else if (position = 1) {T1.setbackgroundcolor (COlor.parsecolor ("#FFFFFF"));
  T2.setbackgroundcolor (Color.parsecolor ("#FFFF00"));
  T3.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));
  else {T1.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));
  T2.setbackgroundcolor (Color.parsecolor ("#FFFFFF"));
  T3.setbackgroundcolor (Color.parsecolor ("#FFFF00"));

 }
 }
 }; @Override public void OnClick (View v) {//TODO auto-generated a stub switch (V.getid ()) {case r.id.text1://click
  First Memo Mpager.setcurrentitem (0, false);
 Break
  Case r.id.text2://Click on the second sticky note mpager.setcurrentitem (1, false);
 Break
  Case r.id.text3://Click on the third sticky note mpager.setcurrentitem (2, false);
 Break
 Default:break;

 }
 }

}

Custom Myviewpager

Package Com.example.myviewpager;
Import Android.content.Context;
Import Android.support.v4.view.ViewPager;
Import Android.util.AttributeSet;

Import android.view.MotionEvent;
 /** * Custom Sliding page can be controlled by setting the Iscanscroll parameter to control whether to allow sliding switch page/public class Myviewpager extends Viewpager {/** whether to allow sliding paging, the default can be sliding * *

 Private Boolean iscanscroll = true;
 Public Myviewpager (context, AttributeSet attrs) {Super (context, attrs);
 Public Myviewpager {Super (context);
 public Boolean Iscanscroll () {return iscanscroll;
 /** set whether the page can be slid/public void Setcanscroll (Boolean iscanscroll) {this.iscanscroll = Iscanscroll;
 @Override public void Scrollto (int x, int y) {super.scrollto (x, y);
 }//Set key to prevent sliding @Override public boolean ontouchevent (Motionevent arg0) {if (!iscanscroll) return true;
 Return super.ontouchevent (arg0);
 @Override public boolean onintercepttouchevent (Motionevent arg0) {return super.onintercepttouchevent (arg0); } @Override PublIC void Setcurrentitem (int item, Boolean smoothscroll) {Super.setcurrentitem (item, Smoothscroll);
 @Override public void Setcurrentitem (int item) {Super.setcurrentitem (item);

 }

}

Adapter:

Package Com.example.myviewpager;

Import java.util.List;
Import Android.support.v4.view.PagerAdapter;
Import Android.view.View;
Import Android.view.ViewGroup; 
  
  /** * Viewpager Adapter * * */public class Myviewpageradapter extends pageradapter{private list<view> mlistviews; 
  Public Myviewpageradapter (list<view> mlistviews) {this.mlistviews = mlistviews;//construction method, parameter is our page card, this is more convenient @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removev Iew (Mlistviews.get (position))//delete page card} @Override public Object instantiateitem (viewgroup container, int position
  {//This method is used to instantiate the page card Container.addview (mlistviews.get (position), 0);//Add page card return mlistviews.get (position);  @Override public int GetCount () {returns mlistviews.size ();//return page card number} @Override public

 Boolean Isviewfromobject (View arg0, Object arg1) {return arg0==arg1;//the official prompt to write}}

First view

Package Com.example.myviewpager;

Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;

/**
 * The first
 * @author CGX * * * * * *
 /Public
class View1 {

 private context mcontext;
 Private View Rootview;
 Public View1 (Context Mcontext) {
 //TODO auto-generated constructor stub
 this.mcontext=mcontext;
 Load layout
 Rootview = Layoutinflater.from (Mcontext). Inflate (
  r.layout.view1_layout, null);
 
 Public View GetView () {return
 Rootview
 }}
 


Activity layout File

<linearlayout 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 "Android:o"
    rientation= "vertical" > <linearlayout android:id= "@+id/linearlayout1" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:background= "#FFFFFF" > <textview android:id= "@+id/te"
      XT1 "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:layout_weight=" "1.0" android:gravity= "Center" android:text= "page card 1" android:textcolor= "#000000" android:textsize= "20SP"/&

    Gt <textview android:id= "@+id/text2" android:layout_width= fill_parent "android:layout_height=" Fill_pare NT "android:layout_weight=" 1.0 "android:gravity=" Center "android:text=" page Card 2 "android:textcolor=" #00

  0000 "android:textsize=" 20sp "/>  <textview android:id= "@+id/text3" android:layout_width= fill_parent "android:layout_height=" Fill_pa Rent "android:layout_weight=" 1.0 "android:gravity=" Center "android:text=" page Card 3 "android:textcolor=" # 000000 "android:textsize=" 20sp "/> </LinearLayout> <com.example.myviewpager.myviewpager Android : id= "@+id/vpager" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_grav
 

ity= "Center" android:layout_weight= "1.0" android:background= "#000000" android:flipinterval= ""/>

 </LinearLayout>

Layout of the View1

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical"
  android:background= "#123456" >
</LinearLayout>

Because it is used for demos, the layout of the view is only differentiated by different background colors, and the layout of the details to be displayed in the development can be changed directly in the view layout file. The view in the demo is to refer to the first one to write, similar

Summarize.

In actual development, although the official provided a lot of APIs, really wait to use their own time, or their own in accordance with the habit of packaging a layer, become their own tools, so that can be directly used, the above example is my internship during the summary, the meanness of the place please forgive, also welcome you to point out, learn together (^_^)

Code Link: http://pan.baidu.com/s/1pJAF6Gz

The above is to the Android Viewpager data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.