刚做完一个比赛项目,来写点以后能用着的东西–Android客户端的首次启动页面,而且这个以后复用的几率很大,也不怎么修改,特留下为以后准备,同时为初学者提供一个帮助。
实现思路是:用SharedPreferences保存一个首次登陆的信息,默认是true,进入MainActivity后对其经行赋flase保存,把GuideActivity作为软件的启动界面,如果是第一次启动,就初始化该activity,不是的话直接跳转到应用主界面activity,这里有些不合理,启动界面如果设置成一个每次app启动都显示的界面最好了,这不是重点,我也就没实现。
首先使用viewpager实现
首先是引导页的布局文件:直接加入ViewPager就可以了,下面的LinearLayout是底部的小圆圈
<?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:orientation="vertical" >
<android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" />
<LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="24.0dp" android:orientation="horizontal" >
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:padding="15.0dip" android:src="@drawable/dot" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:padding="15.0dip" android:src="@drawable/dot" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:padding="15.0dip" android:src="@drawable/dot" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:padding="15.0dip" android:src="@drawable/dot" />
</LinearLayout>
</RelativeLayout>
这个是引导界面的activity代码
package com.example.yasin.ndklearn;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/** * * @{# GuideActivity.java Create on 2013-5-2 下午10:59:08 * * class desc: 引导界面 * * <p> * Copyright: Copyright(c) 2013 * </p> * @Version 1.0 * @Author <a href="mailto:[email protected]">Leo</a> * * */
public class GuideActivity extends AppCompatActivity implements OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views;
// 底部小点图片
private ImageView[] dots;
// 记录当前选中位置
private int currentIndex;
Boolean isFirst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide_layout);
// 初始化页面
initViews();
// 初始化底部小点
initDots();
}
private void initViews() {
SharedPreferences pref = getSharedPreferences("first",Activity.MODE_PRIVATE);
isFirst = pref.getBoolean("status",true);
if(!isFirst){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
LayoutInflater inflater = LayoutInflater.from(this);
RelativeLayout guideFour = (RelativeLayout) inflater.inflate(R.layout.guide_four, null);
guideFour.findViewById(R.id.toMain).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
views = new ArrayList<View>();
// 初始化引导图片列表
views.add(inflater.inflate(R.layout.guide_one, null));
views.add(inflater.inflate(R.layout.guide_two, null));
views.add(inflater.inflate(R.layout.guide_three, null));
views.add(guideFour);
// 初始化Adapter
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
// 绑定回调
vp.setOnPageChangeListener(this);
}
private void initDots() {
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
dots = new ImageView[views.size()];
// 循环取得小点图片
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageView) ll.getChildAt(i);
dots[i].setEnabled(true);// 都设为灰色
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);// 设置为白色,即选中状态
}
private void setCurrentDot(int position) {
if (position < 0 || position > views.size() - 1
|| currentIndex == position) {
return;
}
dots[position].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = position;
}
// 当滑动状态改变时调用
@Override
public void onPageScrollStateChanged(int arg0) {
}
// 当当前页面被滑动时调用
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
// 当新的页面被选中时调用
@Override
public void onPageSelected(int arg0) {
// 设置底部小点选中状态
setCurrentDot(arg0);
}
}
ViewPager中要实现一个ViewpagerAdapter类,对其进行页面的设置
package com.example.yasin.ndklearn;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
/** * Created by Yasin on 2016/3/2. * * * @{# ViewPagerAdapter.java Create on 2013-5-2 下午11:03:39 * * class desc: 引导页面适配器 * * <p> * Copyright: Copyright(c) 2013 * </p> * @Version 1.0 * @Author <a href="mailto:[email protected]">Leo</a> * * */
public class ViewPagerAdapter extends PagerAdapter {
// 界面列表
private List<View> views;
private AppCompatActivity activity;
public ViewPagerAdapter(List<View> views, AppCompatActivity activity) {
this.views = views;
this.activity = activity;
}
//加载viewpager的每个item
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position),0);
return views.get(position);
}
//删除ViewPager的item
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView(views.get(position));
}
// 获得当前界面数
@Override
public int getCount() {
if (views != null) {
return views.size();
}
return 0;
}
//官方推荐这么写,没研究。。。。
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
代码下载地址:https://github.com/yangzhenkun/ViewPager