Android Viewpager Usage
Scenario: When you open an application for the first time, the program will have a hint page to show what the application is capable of, or when the program is updated and what new features are updated, you can use the Viewpager
Demo
Description: Achieve three interface switching, the page color is red, green, blue
Activity_main:
The main interface only adds Viewpager this one control
Android:overscrollmode= the "Never" function is to remove the shadow when the page has been slid to the left or right
<?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.support.v4.view.ViewPager android:id= "@+id/view_pager" android: Layout_width= "Match_parent" android:layout_height= "Match_parent" android:o Verscrollmode= "Never" > </android.support.v4.view.ViewPager></LinearLayout>
Item_image_view:
Viewpager inside the layout file showing content, there is only one ImageView
<?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:orientation= "vertical" > <ImageView android:id= "@+id/item_image" android:layout_width= "Match_parent" android:layout_height= "Match_parent"/> </LinearLayout>
Mainactivity:
PackageCom.zy.viewpager;ImportAndroid.graphics.Color;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsappcompatactivity {
Store View List<View>viewlist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Viewlist=NewArraylist<>(); {
Get Item_image_view Layout File View View= Layoutinflater.from ( This). Inflate (R.layout.item_image_view,NULL);
Find ImageView ImageView based on the Item_image_view layout file ImageView=(ImageView) View.findviewbyid (r.id.item_image);
Add background color Imageview.setbackgroundcolor (color.red) to ImageView;
Add to list inside Viewlist.add (view); } {View View= Layoutinflater.from ( This). Inflate (R.layout.item_image_view,NULL); ImageView ImageView=(ImageView) View.findviewbyid (r.id.item_image); Imageview.setbackgroundcolor (Color.green); Viewlist.add (view); } {View View= Layoutinflater.from ( This). Inflate (R.layout.item_image_view,NULL); ImageView ImageView=(ImageView) View.findviewbyid (r.id.item_image); Imageview.setbackgroundcolor (Color.Blue); Viewlist.add (view); }//Get Viewpager Viewpager Viewpager=(Viewpager) Findviewbyid (R.id.view_pager); Set the adapter for Viewpager, new an anonymous inner class, implement the inside abstract method, and override the Instantiateitem and Destroyitem methods Viewpager.setadapter (NewPageradapter () {
//Return total data length @Override Public int GetCount() {returnviewlist.size (); }//@Override Public Boolean isviewfromobject(View view, Object object) {returnView = =object; } //Load the content displayed on the current page (view) @Override PublicObjectInstantiateitem(ViewGroup container,intposition) {Container.addview (Viewlist.get (position)); returnViewlist.get (position); } //Destroy the view @Override that has slipped past Public void Destroyitem(ViewGroup container,intposition, Object object) {Container.removeview (Viewlist.get (position)); } }); }}
Final effect:
Android Viewpager Usage