Android Boot page uses Viewpager to implement _android

Source: Internet
Author: User

When we install an app, we basically have a guide page that can advertise, or introduce new features and instructions.
It is generally supported by sliding and there are several points below, showing how many pages and the current picture are in place, which is simpler to implement on iOS, but how to implement it on Android.
Today, we will learn to use the Official V4 support Package Viewpager to achieve this effect.

First figure:

Here is my implementation, an XML layout, a guideactivity and a guidepageadapter.
First on XML.

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout 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" tools:context= "com.example.nanchen.guidepagedemo.GuideActivity" > < Android.support.v4.view.ViewPager android:id= "@+id/guide_vp" android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" > </android.support.v4.view.ViewPager> <linearlayout android:id= "@+id/guide_ll_
 Point "android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal " Android:layout_alignparentbottom= "true" android:layout_marginbottom= "40DP" android:gravity= "Center_horizontal" > </LinearLayout> <imagebutton android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:id=" @+id/guide_ib_start "android:src=" @mipmap/btn_start "Android:layout_centerhorizontAl= "true" android:layout_above= "@+id/guide_ll_point" android:background= "@null" android:visibility= "Gone"/>

 </RelativeLayout>

In preparation for a adapter, this is nothing to say.  

Package Com.example.nanchen.guidepagedemo;
Import Android.support.v4.view.PagerAdapter;
Import Android.view.View;
Import Android.view.ViewGroup;

Import java.util.List;

 /** * Created by South Dust on 2016/7/10.
 * * public class Guidepageadapter extends Pageradapter {private list<view> viewlist;

 Public Guidepageadapter (list<view> viewlist) {this.viewlist = viewlist;
 /** * @return Returns the number of pages */@Override public int GetCount () {if (viewlist!= null) {return viewlist.size ();
 return 0; /** * Determines whether the object generates an interface * @param view * @param Object * @return * * @Override public boolean isviewfromobject (view view

 , object) {return view = = object; /** * Initialize Position interface * @param container * @param position * @return * * @Override public Object Instantiateit
 EM (viewgroup container, int position) {Container.addview (Viewlist.get (position));

 return Viewlist.get (position); @Override public void Destroyitem (ViewGroup container, int position, OBJECT object) {Container.removeview (Viewlist.get (position)); 

 }
}

The end is our activity.

Package Com.example.nanchen.guidepagedemo;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.support.v4.view.ViewPager;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.Window;
Import Android.widget.ImageButton;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import java.util.ArrayList;
Import java.util.List;
 
 /** * Implement first boot page/public class Guideactivity extends appcompatactivity implements viewpager.onpagechangelistener{
 Private Viewpager VP; private int []imageidarray;//image resource Array private list<view> viewlist;//Picture resource Collection Private viewgroup vg;//Place dot//instantiate Origin vie
 W Private ImageView Iv_point;
 Private ImageView []ivpointarray;
 The last page of the button private ImageButton ib_start;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Supportrequestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_gUide);
 Ib_start = (ImageButton) Findviewbyid (R.id.guide_ib_start); Ib_start.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {startactivity (new I
  Ntent (Guideactivity.this,mainactivity.class));
  Finish ();
 }

 }); 

 Loading Viewpager initviewpager ();
 Load Bottom dot initpoint (); /** * Load Bottom dot */private void Initpoint () {//here instantiate linearlayout VG = (viewgroup) Findviewbyid (R.id.guide_ll_po

 int);

 Instantiate an array ivpointarray = new Imageview[viewlist.size () based on the number of item Viewpager;
 Loops the new bottom dot ImageView, saves the generated imageview to the array int size = Viewlist.size ();
  for (int i = 0;i<size;i++) {iv_point = new ImageView (this);
  Iv_point.setlayoutparams (New Viewgroup.layoutparams (20,20));
  Iv_point.setpadding (30,0,30,0);//left,top,right,bottom ivpointarray[i] = iv_point;

  The first page needs to be set to the selected state, where two different pictures are used if (i = = 0) {iv_point.setbackgroundresource (R.mipmap.full_holo);
  }else{Iv_point.setbackgroundresource (R.mipmap.empty_holo); }//Imagevi in arrayEW joined to ViewGroup Vg.addview (Ivpointarray[i]);
 }/** * Load picture Viewpager/private void Initviewpager () {VP = (Viewpager) Findviewbyid (R.ID.GUIDE_VP);
 Materialized picture Resource Imageidarray = new INT[]{R.MIPMAP.GUIDE1,R.MIPMAP.GUIDE2,R.MIPMAP.GUIDE3};
 Viewlist = new arraylist<> ();
  Gets a layout parameter, set to full screen linearlayout.layoutparams params = new Linearlayout.layoutparams (
 Linearlayout.layoutparams.match_parent,linearlayout.layoutparams.match_parent);
 Loop create view and add to the collection int len = imageidarray.length;
  for (int i = 0;i<len;i++) {//new ImageView and set full screen and picture resources ImageView ImageView = new ImageView (this);
  Imageview.setlayoutparams (params);
  Imageview.setbackgroundresource (Imageidarray[i]);
 Adding ImageView to the Set Viewlist.add (ImageView);
 When the//view collection is initialized, set the adapter Vp.setadapter (new Guidepageadapter (viewlist));
 Set Sliding monitoring Vp.setonpagechangelistener (this); @Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {}/** * Sliding supervisor Listen to * @paramPosition/@Override public void onpageselected (int position) {//loop to set the current page's tag graph int length = imageidarray.length;
  for (int i = 0;i<length;i++) {ivpointarray[position].setbackgroundresource (R.mipmap.full_holo);
  if (position!= i) {ivpointarray[i].setbackgroundresource (R.mipmap.empty_holo);
 }//To determine if it is the last page, if the display button if (position = = imageidarray.length-1) {ib_start.setvisibility (view.visible);
 }else {ib_start.setvisibility (view.gone); 

 @Override public void onpagescrollstatechanged (int state) {}}

In general, the implementation process is relatively simple.
1 Load the Viewpager first, create the new ImageView and add it to the View collection, and then set the adapter and display it.
2 then load the LinearLayout, place the original point picture, the same new ImageView set the background and put in the dot chart collection.
3 finally set the Viewpager sliding monitor event, in the sliding completion of the listening onpageselected method, set the corresponding dot for selected, if the last page, then show the button.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.