Viewpager is a class in the Android expansion pack V4 that allows you to switch the current view left and right with the user.
Characteristics:
1.viewPager inherits the ViewGroup class directly, so it is a container class where you can add other view
The 2.viewPager class requires a Pageradapter adapter class to provide him with data
3.viewPager is often used together with fragment, and offers specialized fragmentpageradapter and Fragmentstatepageradapter classes
Idea: In the use of writing Viewpager, you also need to use two components, respectively, Pagertitlestrip and Pagertabstrip classes, Pagertitlestrip class directly integrates the ViewGroup class, And Pagertabstrip inherits the Pagertitlestrip class, so these two classes are also container classes
Code xml:
<android.support.v4.view.viewpager
Android:id= "@+id/viewpager"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<android.support.v4.view.pagertabstrip
Android:id= "@+id/tabstrip"
Android:layout_width= "Wrap_content"
Android:layut_heigth= "50dip"
android:gravity= "Center"/>
</android.support.v4.view.ViewPager>
Mainactivity.java:
ArrayList<View> viewContainter =
new
ArrayList<View>();
ArrayList<String> titleContainer =
new
ArrayList<String>();
Cancel the long line below the tab
Tabstrip.setdrawfullunderline (flase);
Set the background color of the tab
Tabstrip.setbackgroundcolor (This.getresources (). GetColor (r.color.red));
/sets the underline color of the current tab page
Tabstrip.settabindicatorcolor (This.getresources (). GetColor (r.color.red));
Tabstrip.settextspacing (200);
View view1 = LayoutInflater.from(
this
).inflate(R.layout.tab1,
null
);
View view2 = LayoutInflater.from(
this
).inflate(R.layout.tab2,
null
);
View view3 = LayoutInflater.from(
this
).inflate(R.layout.tab3,
null
);
View view4 = LayoutInflater.from(
this
).inflate(R.layout.tab4,
null
);
//viewpager开始添加view
viewContainter.add(view1);
viewContainter.add(view2);
viewContainter.add(view3);
viewContainter.add(view4);
//页签项
titleContainer.add(
"网易新闻"
);
titleContainer.add(
"网易体育"
);
titleContainer.add(
"网易财经"
);
titleContainer.add(
"网易女人"
);The number of components in Pager.setadapter (new Pageradater () {//viewpager public int getcount () {return viewcontainter.size (); }//Sliding switch should destroy the current component public void Destroyitem (ViewGroup container,int position,object Object) {(Viewpager) containe R). Removeview (Viewcontainter.get (position)); }//slide should be generated for the component public Object Instantiateitem (viewgroup container,int position) {((Viewpager) container). AddView (Viewcontainter.get (position)); return Viewcontainter.get (position); }//must override the method public boolean isviewfromobject (View arg0,object arg1) {return arg-==arg1; } public int GetItemPosition (Object object) {return Super.getitemposition (object); } public charsequence getpagetitle (int position) {return titlecontainer.get (position); }});//Swipe to listen
pager.setOnPageChangeListener(
new
OnPageChangeListener() {
@Override
public
void
onPageScrollStateChanged(
int
arg0) {
Log.d(TAG,
"--------changed:"
+ arg0);
}
@Override
public
void
onPageScrolled(
int
arg0,
float
arg1,
int
arg2) {
Log.d(TAG,
"-------scrolled arg0:"
+ arg0);
Log.d(TAG,
"-------scrolled arg1:"
+ arg1);
Log.d(TAG,
"-------scrolled arg2:"
+ arg2);
}
@Override
public
void
onPageSelected(
int
arg0) {
Log.d(TAG,
"------selected:"
+ arg0);
}
});Idea: For the Pargeradapter class, you must override 4 methods: Public Object Instantiateitem (viewgoup container,int position) {};p ublic void Destroyitem (ViewGroup container,int postion, Object object) {};p ublic int getcount () {};p ublic Boolean isviewfromobject ( View arg0,object arg1) {}; :
Basic use of Viewpager