Android Viewpager Usage

Source: Internet
Author: User

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

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.