Android Tricks one: Start screen + new features left and right navigation logic

Source: Internet
Author: User

Objective

For a long time did not write a blog, do not write something really justified, the work of some valuable things to sort out to share, in the present also a bit of timeliness, or sooner or later will rot in the belly. Remember before the wizard has an open source program is to achieve one weeks to develop the app, now pick it up, the plan is not implemented with my lazy body related, any great things need a strong executive power to achieve, slow a little matter, can create something is worth the thing.

This blog first introduces the most common features of an app, that is, the introduction of new features and the Start screen, the general will be how to implement it, this is not intended to tell you.

First, logic.
    1. First to determine whether to start the app for the first time, if so, then enter the function to use navigation (the simplest way is to swipe left and right to view, swipe to the last page click the button to enter the homepage).
    2. If not, display the splash screen and enter the home page after 2 seconds.

Logic is very simple, what if there is advertising? Ads must be taken from the server, but will be cached to the local, no network time can be displayed, you can use WebView to display ads, anyway the author is such a dry, concrete implementation first not said.

Look at the effect

On the Code Splashactivity.java
 PackageCom.devilwwj.featureguide;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importcom.devilwwj.featureguide.global.AppConstants;ImportCom.devilwwj.featureguide.utils.SpUtils;/** * @desc Start screen * Created by DEVILWWJ on 16/1/23. * * Public  class splashactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Determine if it is the first time to open the app        BooleanIsfirstopen = Sputils.getboolean ( This, Appconstants.first_open);//If it is the first time, enter the function guide page first        if(!isfirstopen) {Intent Intent =NewIntent ( This, Welcomeguideactivity.class);            StartActivity (Intent); Finish ();return; }//If the app is not launched for the first time, the splash screen displays normallySetcontentview (R.layout.activity_splash);NewHandler (). postdelayed (NewRunnable () {@Override             Public void Run() {enterhomeactivity (); }        }, -); }Private void enterhomeactivity() {Intent Intent =NewIntent ( This, Mainactivity.class);        StartActivity (Intent);    Finish (); }}

Code parsing: Use Sharedpreference to save app startup state, if true, enter function navigation, otherwise delay 2 seconds before entering the main page.

Welcomeguideactivity.java
 PackageCom.devilwwj.featureguide;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.LinearLayout;Importcom.devilwwj.featureguide.global.AppConstants;ImportCom.devilwwj.featureguide.utils.SpUtils;ImportJava.util.ArrayList;ImportJava.util.List;/** * Welcome Page * * @author wwj_748 * * */ Public  class welcomeguideactivity extends Activity  implements  Onclicklistener {    PrivateViewpager VP;PrivateGuideviewpageradapter adapter;Privatelist<view> views;PrivateButton startbtn;//Guide page picture resource    Private Static Final int[] pics = {r.layout.guid_view1, r.layout.guid_view2, R.LAYOUT.GUID_VIEW3, r.layout.guid_view4};//Bottom small dot picture    PrivateImageview[] dots;//Record the currently selected location    Private intCurrentindex;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_guide); views =NewArraylist<view> ();//Initialize the Boot Page view list         for(inti =0; i < pics.length; i++) {View view = Layoutinflater.from ( This). Inflate (Pics[i],NULL);if(i = = pics.length-1{startbtn = (Button) View.findviewbyid (R.id.btn_login); Startbtn.settag ("Enter"); Startbtn.setonclicklistener ( This);        } views.add (view); } VP = (Viewpager) Findviewbyid (r.id.vp_guide);//Initialize adapteradapter =NewGuideviewpageradapter (views);        Vp.setadapter (adapter); Vp.setonpagechangelistener (NewPagechangelistener ());    Initdots (); }@Override    protected void Onresume() {Super. Onresume (); }@Override    protected void OnPause() {Super. OnPause ();//If you switch to the background, set the next time you do not enter the feature Guide pageSputils.putboolean (welcomeguideactivity. This, Appconstants.first_open,true);    Finish (); }@Override    protected void OnStop() {Super. OnStop (); }@Override    protected void OnDestroy() {Super. OnDestroy (); }Private void Initdots() {LinearLayout LL = (linearlayout) Findviewbyid (R.ID.LL); Dots =NewImageview[pics.length];//loop to get the dot picture         for(inti =0; i < pics.length; i++) {//Get a linearlayout below each sub-elementDots[i] = (ImageView) ll.getchildat (i); Dots[i].setenabled (false);//are set to grayDots[i].setonclicklistener ( This); Dots[i].settag (i);//Set position tag for easy removal and corresponding to current position} Currentindex =0; Dots[currentindex].setenabled (true);//Set to white, select State}/** * Set Current View * * @param Position */    Private void Setcurview(intPosition) {if(Position <0|| Position >= pics.length) {return;    } vp.setcurrentitem (position); }/** * Set Current indication point * * @param Position */    Private void Setcurdot(intPosition) {if(Position <0|| Position > Pics.length | | Currentindex = = position) {return; } dots[position].setenabled (true); Dots[currentindex].setenabled (false);    Currentindex = position; }@Override     Public void OnClick(View v) {if(V.gettag (). Equals ("Enter")) {entermainactivity ();return; }intPosition = (Integer) v.gettag ();        Setcurview (position);    Setcurdot (position); }Private void entermainactivity() {Intent Intent =NewIntent (welcomeguideactivity. This, Splashactivity.class);        StartActivity (Intent); Sputils.putboolean (welcomeguideactivity. This, Appconstants.first_open,true);    Finish (); }Private  class Pagechangelistener implements Onpagechangelistener {        //Called when sliding state changes        @Override         Public void onpagescrollstatechanged(intPosition) {//arg0 ==1 The time implied is sliding, the arg0==2 of the hour of the tacit slide finished, the arg0==0 of the hour implied that nothing has been done. }called when the current page is being slid        @Override         Public void onpagescrolled(intPositionfloatArg1,intARG2) {//arg0: The current page, and the page you clicked to swipe            //arg1: Percentage of current page offset            //arg2: The pixel position of the current page offset}//Called when a new page is selected        @Override         Public void onpageselected(intPosition) {//Set bottom dot check stateSetcurdot (position); }    }}

Code parsing: Left and right swipe is done using Viewpager, switch 4 different views, monitor the Viewpager of the page switch events to change the bottom point of the switch, swipe to the last page, set the button click event, click to enter the home page.

GitHub

More details on the code, we look at the source project, the code has been uploaded to GitHub, welcome everyone down to use.
One week development app

It_xiao, the Little Witch
Blog Address: http://blog.csdn.net/wwj_748

Android Tricks one: Start screen + new features left and right navigation logic

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.