Android Tips One Start screen + new function left and right navigation logic _android

Source: Internet
Author: User

Objective

For a long time did not write a blog, do not write something really unreasonable, the work of some valuable things sorted out to share, in the present still a bit of timeliness, or sooner or later will rot in the belly. Remember when the wizard had an open source plan to implement the one-week development app, now pick it up, the plan has not been carried out with my lazy body, any great things need a strong executive power to achieve, slow it doesn't matter, can create something is worth it.

This blog first introduces the most common features of an app, that is, the introduction of new features and the launch screen, the general how to achieve it, this is not going to tell you.

Logic first.

First to determine whether the first launch of the app, if it is, then enter the function to use navigation (the simplest way is, left and right sliding switch view, slide to the last page click button to enter the homepage).

If not, display the splash screen, and then enter the home page in 2 seconds.

The logic is very simple, what if there is advertising? Advertising must be taken from the server, but will be cached to the local, no network can be displayed, you can use WebView to display ads, anyway, the author is doing so, concrete implementation first.

Look at the effect


On the Code

Splashactivity.java

Package com.devilwwj.featureguide;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import com.devilwwj.featureguide.global.AppConstants;
Import Com.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.onc
Reate (savedinstancestate);
Judge whether it is the first time to open the application Boolean Isfirstopen = Sputils.getboolean (this, appconstants.first_open); If this is the first time you start, go to the Functional boot page if (!isfirstopen) {Intent Intent = new Intent (this, welcomeguideactivity.class); startactivity (int
ENT);
Finish ();
Return
///If it is not the first time the app is started, the boot screen Setcontentview (r.layout.activity_splash) is displayed normally;
New Handler (). postdelayed (New Runnable () {@Override public void run () {enterhomeactivity ();}}, 2000); private void Enterhomeactivity () {Intent Intent = new Intent (this, mainactivity.class); startactivity (Intent); Finish ()
; Code resolution: Using ShareDpreference to save the app startup state and, if true, go to feature navigation, or delay 2 seconds before entering the main page.
Welcomeguideactivity.java package com.devilwwj.featureguide;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import com.devilwwj.featureguide.global.AppConstants;
Import Com.devilwwj.featureguide.utils.SpUtils;
Import java.util.ArrayList;
Import java.util.List; /** * Welcome Page * * * @author wwj_748 */public class Welcomeguideactivity extends activity implements Onclicklistener {Priva
Te Viewpager VP;
Private Guideviewpageradapter adapter;
Private list<view> views;
Private Button startbtn; Guide page picture resources private static final int[] Pics = {r.layout.guid_view1, r.layout.guid_view2, r.layout.guid_VIEW3, R.LAYOUT.GUID_VIEW4};
Bottom dot picture private imageview[] dots;
Record the current selected location private int currentindex; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_guide);
views = new arraylist<view> (); Initializes a list of boot page views for (int i = 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 Adapter adapter = new Guideviewpageradapter (views);
Vp.setadapter (adapter);
Vp.setonpagechangelistener (New Pagechangelistener ());
Initdots (); @Override protected void Onresume () {Super.onresume ();} @Override protected void OnPause () {super.onpause ();//If you switch to
Background, set the next time not to enter the Function Guide page sputils.putboolean (Welcomeguideactivity.this, Appconstants.first_open, true);
Finish (); } @Override PRotected void OnStop () {super.onstop ();} @Override protected void OnDestroy () {Super.ondestroy ();} private void Initdot S () {linearlayout LL = (linearlayout) Findviewbyid (r.id.ll); dots = new Imageview[pics.length];//loop get dot picture for (int i = 0; i < pics.length; i++) {//Get a LinearLayout each of the following child elements dots[i] = (ImageView) ll.getchildat (i); dots[i].setenabled (false);//all set to gray dots[i].
Setonclicklistener (this);
Dots[i].settag (i)//Set position tag, convenient to remove the corresponding with the current position} currentindex = 0; Dots[currentindex].setenabled (TRUE); Set to White, select State}/** * Set Current View * * @param position/private void Setcurview (int position) {if (Position < 0 | | pos
Ition >= pics.length) {return;} vp.setcurrentitem (position); /** * Sets the current indication point * * @param position/private void Setcurdot (int position) {if (Position < 0 | | position > PICS.L Ength | |
Currentindex = = position) {return;} dots[position].setenabled (True);
Dots[currentindex].setenabled (FALSE);
Currentindex = position; @Override public void OnClick (ViewV) {if (V.gettag (). Equals ("enter")) {entermainactivity (); return;} int position = (Integer) v.gettag (); Setcurview (posi
tion);
Setcurdot (position); private void Entermainactivity () {Intent Intent = new Intent (welcomeguideactivity.this, splashactivity.class); StarTAC
Tivity (Intent);
Sputils.putboolean (Welcomeguideactivity.this, Appconstants.first_open, true);
Finish (); Private class Pagechangelistener implements Onpagechangelistener {//When the sliding state is changed, call @Override public void Onpagescrollstate
Changed (int position) {//arg0 ==1 's Hour is slipping, the arg0==2 's Hour is slipping, and arg0==0 's Hour is silent. ///The current page is sliding when called @Override public void onpagescrolled (int position, float arg1, int arg2) {//arg0: Current page, and you click on the sliding page//arg1 : Percent of current page offset//arg2: The pixel position of the current page offset}//When the new page is selected @Override public void onpageselected (int position) {//Set the bottom dot selected state setcur
Dot (position); }
}
}

Code Analysis: Left-right sliding is used Viewpager to do, switch 4 different view, monitor the Viewpager page toggle event to change the bottom point of the switch, slide to the last page, set the button click event, click to enter the home page.

GitHub

More details on the code, we look at source engineering, the code has been uploaded to GitHub, welcome everyone down to use.


Related Article

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.