Android ViewPager引導頁(三)

來源:互聯網
上載者:User

標籤:viewpager   android   

一:ViewPager和導航點的實現:


主布局為guide.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_height="match_parent"        android:layout_width="match_parent"        android:background="#00000000"        android:persistentDrawingCache="animation">    </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/ll"        android:orientation="horizontal"        android:gravity="center_horizontal"        android:layout_alignParentBottom = "true">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/iv1"            android:src="@drawable/login_point"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/iv2"            android:src="@drawable/login_point_selected"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/iv3"            android:src="@drawable/login_point_selected"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/iv4"            android:src="@drawable/login_point_selected"            />        </LinearLayout></RelativeLayout>

guide.java:

package com.banana.k08.viewpagerdemo1;import android.content.Intent;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.support.v7.app.ActionBarActivity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.util.ArrayList;import java.util.List;/** * Created by bananagg on 2015-07-28. */public class Guide extends ActionBarActivity implements ViewPager.OnPageChangeListener{    private ViewPager vp;    private ViewPagerAdapter vpAdapter;    private List<View> views;    private ImageView[] dots;    private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3,R.id.iv4};    private Button start_btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.guide);        initView();        initDots();    }    private void initView() {        LayoutInflater inflater = LayoutInflater.from(this);        views = new ArrayList<View>();        views.add(inflater.inflate(R.layout.one,null));   //添加圖片        views.add(inflater.inflate(R.layout.two,null));        views.add(inflater.inflate(R.layout.three,null));        views.add(inflater.inflate(R.layout.four,null));        vpAdapter = new ViewPagerAdapter(views,this);        vp = (ViewPager) findViewById(R.id.viewpager);        vp.setAdapter(vpAdapter);        start_btn = (Button) views.get(3).findViewById(R.id.start_btn);        start_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                startActivity(new Intent(Guide.this,MainActivity.class));                finish();            }        });        vp.setOnPageChangeListener(this);              //回呼函數,用於頁面改變的監聽    }    private void initDots(){        dots = new ImageView[views.size()];        for(int i=0;i<views.size();i++){            dots[i] = (ImageView) findViewById(ids[i]);        }    }    @Override    public void onPageScrolled(int i, float v, int i2) {    }    @Override    public void onPageSelected(int arg0) {   //更改當前置航的顏色        for (int i=0;i<ids.length;i++){            if(arg0 == i){                dots[i].setImageResource(R.drawable.login_point);            }            else{                dots[i].setImageResource(R.drawable.login_point_selected);            }        }    }    @Override    public void onPageScrollStateChanged(int i) {    }}

二:設定資料存放區


對於很多APP,只有在安裝軟體是才會有導航頁,在第二次開啟著軟體時是不會繼續載入導航頁的,所以我們需要一個變數來控制這個頁面的跳轉,這兒採用SharedPreferences方式儲存資料,用這個儲存的資料來控制頁面跳轉的變數值。


package com.banana.k08.viewpagerdemo1;import android.content.Intent;import android.content.SharedPreferences;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class WelcomeAct extends ActionBarActivity {    private boolean isFirstIn =false;    private static final int TIME = 2000;    private static final int GO_HOME = 1000;    private static final int GO_GUIDE = 1001;    private Handler mHandler = new Handler(){        public void handleMessage(Message msg){            switch (msg.what){                case GO_GUIDE:                    goGuide();                    break;                case GO_HOME:                    goHome();                    break;            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_welcome);        init();    }    private void init(){        SharedPreferences perPreferences = getSharedPreferences("jike",MODE_PRIVATE);    //MODE_PRIVATE表示該檔案只能被自身應用調用        isFirstIn = perPreferences.getBoolean("isFirstIn",true);   //        System.out.println(isFirstIn);        if(!isFirstIn){            mHandler.sendEmptyMessageDelayed(GO_HOME,TIME);        }        else{            mHandler.sendEmptyMessageDelayed(GO_GUIDE,TIME);            SharedPreferences.Editor editor =  perPreferences.edit();    //取得編輯            editor.putBoolean("isFirstIn",false);                        //提交資料            editor.commit();                                             //已經解鎖註冊,所以更改資料時不會觸發監聽器        }    }    private void goHome(){        Intent i = new Intent(WelcomeAct.this,MainActivity.class);        startActivity(i);        finish();    }    private void goGuide(){        Intent i = new Intent(WelcomeAct.this,Guide.class);        startActivity(i);        finish();    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android ViewPager引導頁(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.