Android Development using Viewpager to do the boot page

Source: Internet
Author: User

Guide Page I believe we will not be unfamiliar, installed a new app after the first open, there will be similar, equivalent to the description of the document


Achieve results


Program directory Structure


Main.xml define a frame layout in the main layout and how many points are displayed on the Viewpager

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <android.support.v4.view.viewpager android:id= "@+id/viewpager" android:layout_width= "match_parent "Android:layout_height=" match_parent "android:layout_gravity=" center "> </android.support.v4.view.        viewpager> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content"            Android:layout_gravity= "Bottom|center_horizontal" android:orientation= "Horizontal" > <ImageView Android:id= "@+id/icon_1" android:layout_width= "wrap_content" android:layout_height= "Wrap_            Content "android:layout_marginright=" 30DP "android:contentdescription=" @string/content_description " android:src= "@drawable/paGe_icon "/> <imageview android:id=" @+id/icon_2 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_marginright= "30DP" Android:contentdescri ption= "@string/content_description" android:src= "@drawable/page_icon"/> <imageview and            Roid:id= "@+id/icon_3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:contentdescription= "@string/content_description" android:src= "@drawable/page_icon"/> </ Linearlayout></framelayout>

The layout of each view Pager1.xml

<?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: background= "@drawable/image1"    android:orientation= "vertical" >    <button android:id=        "@+id/btn_ In_first "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:layout_ gravity= "center"        android:text= "first page"/></linearlayout>
Other two view similar

In the main activity

Package Com.example.viewpagerdemo;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.support.v4.view.pageradapter;import Android.support.v4.view.ViewPager; Import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.layoutinflater;import Android.view.view;import Android.widget.imageview;public class Mainactivity extends Activity {private Viewpager Viewpager = Null;private ImageView img1, Img2, img3;private arraylist<string> titles;private int curindex =-1; @Over rideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Initwidgets ();//Put the view you want to display into the array layoutinflater li = Layoutinflater.from (this); View view1 = li.inflate (r.layout.pager1, NULL); View view2 = li.inflate (r.layout.pager2, NULL); View view3 = li.inflate (R.layout.pager3, NULL);//Add page final arraylist<view> views = new arraylist<view> (); Views.add (View1); Views.add (view2); Views.add (VIEW3);//Add title titles = new Arraylist<string> () Titles.add ("Tab1"); Titles.add ("tab2"); Titles.add ( "Tab3");p icviewpageradapter pageradapter = new Picviewpageradapter (views); Viewpager.setadapter (Pageradapter); Curindex = 1;viewpager.setcurrentitem (Curindex); Img2.setimageresource (R.drawable.page_icon_sel); Viewpager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {//TODO Auto-generated method Stubswitch (arg0) {case 0:img1.setimageresource (R.drawable.page_icon_sel); Img2.setimageresource (R.drawable.page_icon); Img3.setimageresource (R.drawable.page_icon); Break;case 1: Img2.setimageresource (R.drawable.page_icon_sel); Img1.setimageresource (R.drawable.page_icon); Img3.setimageresource (R.drawable.page_icon); Break;case 2:img3.setimageresource (R.drawable.page_icon_sel); Img2.setimageresource (R.drawable.page_icon); Img1.setimageresource (R.drawable.page_icon); break;default:break;} Curindex = arg0; System.out.println ("[Mainactivity->]currinDex = "+ Curindex);} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic vo ID onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); private void Initwidgets () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); img1 = (ImageView) Findviewbyid ( r.id.icon_1); img2 = (ImageView) Findviewbyid (r.id.icon_2); img3 = (ImageView) Findviewbyid (r.id.icon_3);} /** * Add Adapter for Viewpager * * @author Administrator * */class Picviewpageradapter extends Pageradapter {private List<view > Listviews;public picviewpageradapter (list<view> list) {listviews = list;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn listviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated method Stubreturn arg0 = arg1;} @Overridepublic void Destroyitem (View container, int position, object object) {(Viewpager) container). Removeview ( Listviews.get (position));} @OveRridepublic charsequence getpagetitle (int position) {return titles.get (position);} @Overridepublic Object Instantiateitem (View container, int position) {((Viewpager) container). AddView (Listviews.get ( position)); return listviews.get (position);}}
Each small dot is changed by the Viewpager listener.

Viewpager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {//TODO Auto-generated method Stubswitch (arg0) {case 0:img1.setimageresource (R.drawable.page_icon_sel); Img2.setimageresource (R.drawable.page_icon); Img3.setimageresource (R.drawable.page_icon); Break;case 1: Img2.setimageresource (R.drawable.page_icon_sel); Img1.setimageresource (R.drawable.page_icon); Img3.setimageresource (R.drawable.page_icon); Break;case 2:img3.setimageresource (R.drawable.page_icon_sel); Img2.setimageresource (R.drawable.page_icon); Img1.setimageresource (R.drawable.page_icon); break;default:break;} Curindex = arg0; System.out.println ("[Mainactivity->]currindex =" + Curindex);} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic vo ID onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}});

Full demo Download: http://download.csdn.net/detail/deng0zhaotai/7272609

Android Development using Viewpager to do the boot page

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.