一: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();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010304217/article/details/47110427