Android Winter Training Cloud Notes Summary--Welcome page

Source: Internet
Author: User

The Welcome page uses Viewpager and requires an adapter.

Note the point:

1. Decide whether to enter this app for the first time.

2, welcome the small dot logic.

Implementation principle:

First put in the Activity_welcome Viewpager and fixed four small dots in the picture below.

Viewpager a diagram for storing multiple welcome pages.

The welcome_selectoer is used when the small dot on which page corresponds to a gray state. That is, when set to not point, the status is gray.

The contents of the Viewpager are statically loaded in.

Direct code, Code has comments:

Welcomeactivity.java

 Packagecom.app.note.activity;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.util.Log;ImportAndroid.view.Display;ImportAndroid.view.View;ImportCOM.APP.NOTE.R;ImportCom.app.note.adapter.WelcomeAdapter;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;Importandroid.view.animation.Animation;Importandroid.view.animation.ScaleAnimation;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.logging.Handler; Public classWelcomeactivityextendsActivityImplementsOnpagechangelistener,view.onclicklistener {PrivateViewpager Viewpager; PrivateList<view>viewlist; PrivateView View1, View2, VIEW3, view4; PrivateImageView points[];//Storing small circle arrays    Private intcurrentindex=0;//current page, default home    PrivateButton Startbutton; PrivateBoolean Isfirstin =false; Privatesharedpreferences pref; PrivateImageView Doge; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //set the picture to full screenGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN        ); //Hide title bar         This. Requestwindowfeature (Window.feature_no_title); //decide whether to use the app for the first time//The first parameter is the file name, and the second parameter is the operation mode       /*context.mode_private: As the default mode of operation, which means that the file is private data and can only be accessed by the app itself, in which the content of the write overwrites the contents of the original file, if you want to append the newly written content to the original file.        You can use Context.mode_append context.mode_append: Mode checks whether a file exists, appends content to the file, or creates a new file.        Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file. Mode_world_readable: Indicates that the current file can be read by another application; Mode_world_writeable: Indicates that the current file can be written by another application. */pref= Getapplicationcontext (). Getsharedpreferences ("Firstorno", 0 ); Isfirstin= Pref.getboolean ("Isfirstin",true); if(isfirstin) {Setcontentview (r.layout.activity_welcome); Initviewpager ();//initializing the Viewpager objectInitpoint ();//Initialize the navigation small dotChangefirst (); }Else{Intent Intent=NewIntent (welcomeactivity. This, Loginactivity.class);                    StartActivity (Intent);    Finish (); }}    Private voidChangefirst () {pref= Getapplicationcontext (). Getsharedpreferences ("Firstorno", 0); Sharedpreferences.editor Editor=Pref.edit (); Editor.putboolean ("Isfirstin",false);    Editor.commit (); }    Private voidInitpoint () {linearlayout linearlayout=(LinearLayout) Findviewbyid (R.ID.LL); Points=NewImageview[4];  for(inti=0;i<points.length;i++) {Points[i]= (ImageView) linearlayout.getchildat (i);//Traverse All ImageView sub-nodes under LinearLayoutPoints[i].setenabled (true);//set current status to allow (dot, gray)//Set Click to listenPoints[i].setonclicklistener ( This); //set an additional identifier to jump to the page when you click the dotPoints[i].settag (i);//identifiers are consistent with dot order} currentindex=0; Points[currentindex].setenabled (false);//Set Homepage as current page (dot coloring, blue)Startbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (welcomeactivity. This, Loginactivity.class);                StartActivity (Intent);            Finish ();    }        }); }    Private voidInitviewpager () {Viewpager= (Viewpager) Findviewbyid (R.id.viewpager);//Get Viewpager Instanceviewlist=NewArraylist<view> ();//instantiating a list collectionstartbutton=(Button) Findviewbyid (R.id.startbutton); //add view statically with XMLView1=view.inflate (welcomeactivity. This, R.layout.view1,NULL); View2=view.inflate (welcomeactivity. This, R.layout.view2,NULL); VIEW3=view.inflate (welcomeactivity. This, R.LAYOUT.VIEW3,NULL); View4=view.inflate (welcomeactivity. This, R.LAYOUT.VIEW4,NULL);        Viewlist.add (View1);        Viewlist.add (VIEW2);        Viewlist.add (VIEW3);        Viewlist.add (VIEW4); //setting up the adapterWelcomeadapter adapter=NewWelcomeadapter (viewlist); //Binding AdapterViewpager.setadapter (adapter); //set page tab Toggle ListenerViewpager.setonpagechangelistener ( This); } @Override Public voidOnpagescrollstatechanged (intarg0) {} @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {} @Override Public voidOnpageselected (intPosition) {//when the current page card is selected, position is the current number of pages//hide a point when you swipe to the last page        if(position==3){             for(inti=0;i<=position;i++) {points[i].setvisibility (view.gone); } startbutton.setenabled (true);        Startbutton.setvisibility (view.visible); }        //Show the dots when you slip back from the last page        if(currentindex==3){             for(inti=0;i<=currentindex;i++) {points[i].setvisibility (view.visible); } startbutton.setenabled (false);        Startbutton.setvisibility (View.gone); } points[position].setenabled (false);//not PointPoints[currentindex].setenabled (true);//Restore previous page statuscurrentindex=position; } @Override Public voidOnClick (View v) {Viewpager.setcurrentitem (int) V.gettag ()); }}

Adapter Welcomeadapter.java

Package Com.app.note.adapter;import Android.support.v4.view.pageradapter;import Android.view.view;import Android.view.viewgroup;import java.util.list;/** * Created by Hxdn on 2015/12/29.    */public class Welcomeadapter extends Pageradapter {private list<view> List;    Public Welcomeadapter (list<view> list) {//Use the constructor to receive the list collection parameter this.list = list;        } @Override public int getcount () {//Returns the number of page cards if (list.size ()! = 0) {return list.size ();    } return 0; } @Override Public boolean isviewfromobject (view arg0, Object arg1) {//Determines whether the View object is return arg0==arg1;//official demo Suggested notation} @Override public Object Instantiateitem (ViewGroup container, int position) {//Instantiate a page card, view object stored in Viewgrou        P-Container.addview (List.get (position));    return List.get (position); } @Override public void Destroyitem (ViewGroup container, int position, object object) {//Destroy a page card Container.rem    Oveview (List.get (position)); }}

Layout code: Activity_welcome.xml

 <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.app.note.activity.WelcomeActivity" > <android.support.v4.view.viewpager android:id= "@+id/viewpager" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent"/> <linearlayout Andro Id:id= "@+id/ll" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layo        Ut_alignparentbottom= "true" android:layout_centerhorizontal= "true" android:layout_marginbottom= "20DP" android:orientation= "Horizontal" > <imageview android:id= "@+id/iv1" Android:layout_wid            Th= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" Android:clickable= "true" android:padding= "25DP" android:src= "@drawable/welcome_selector"/> <imageview android:id = "@+id/iv2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android Oid:layout_gravity= "Center_vertical" android:clickable= "true" android:padding= "25DP" Android oid:src= "@drawable/welcome_selector"/> <imageview android:id= "@+id/iv3" android:layout            _width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" Android:clickable= "true" android:padding= "25DP" android:src= "@drawable/welcome_selector" /> <imageview android:id= "@+id/iv4" android:layout_width= "Wrap_content" Android            oid:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:clickable= "true" Android:paddinG= "25DP" android:src= "@drawable/welcome_selector"/> </LinearLayout> <button android:i        D= "@+id/startbutton" android:layout_width= "160DP" android:layout_height= "50DP" android:text= "click to enter" Android:textsize= "20DP" android:textcolor= "@drawable/button_text" style= "@style/btn_login" Andro id:background= "@drawable/bg_login_btn_qq" android:visibility= "Gone" android:clickable= "false" Android : layout_above= "@+id/ll" android:layout_centerhorizontal= "true"/></relativelayout>

Four pages because it's almost just one copy of the 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 ">    <imageview        android:id= "@+id/iv1"        android:layout_width= "match_parent"        android:layout_height= "Match_parent"        android:layout_gravity= "center_vertical"        android:clickable= "true"        android:src= "@drawable/welcome1 "        android:scaletype=" Fitxy "/></linearlayout>

XML for the most important selector:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item        android:state_enabled=" true "android:drawable=" @drawable/v_point_pre "        ></item>    <item        android:state_enabled= "false" android:drawable= "@drawable/v_ Point_gray "        ></item></selector>

Android Winter Training Cloud Notes Summary--Welcome 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.