GuidePager引导页小白入门解析
第一步创建一个SplashActivity引导页,此类已经打包成工具类以及对应的Xml
package com.MrYu.Main;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class SplashActivity extends Activity {
/**
* 自定义俩个方法
* IsFinish判断是否首次登陆类
* 第一个时间的方法Handler线程switch方法
* TIME=3s为Splash跳转至Guide
* Splash=1s为Splash跳转主页面Main
* 第二个为判断是否第一次使用Init方法
* GO_GUIDE=100为第一次使用GO_Main=101
* IF ELSE 为循环语句判断
* Date:2018/10/6
*/
private static final int Splash=1000;
private static final int TIME=3000;
private static final int GO_GUIDE=100;
private static final int GO_MAIN=101;
//Handle线程
Handler mhandler=new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case GO_GUIDE:
goGuide();
break;
case GO_MAIN:
goMain();
break;
}
}
//接上俩个方法GoGuide,GoMain 运行完了后Finish结束进程
private void goGuide() {
Intent intent=new Intent(SplashActivity.this,GuideActivity.class);
startActivity(intent);
finish();
}
private void goMain() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在AndroidManifest配置中添加并注册SplashActivity方法
setContentView(R.layout.activity_splash);
//创建一个InIt方法
init();
}
//init方法
private void init() {
SharedPreferences sf=getSharedPreferences("data", MODE_PRIVATE);//判断是否是第一次进入
boolean isFirstIn=sf.getBoolean("isFirstIn", true);
SharedPreferences.Editor editor=sf.edit();
//IF语句判断
if(isFirstIn){ //若为true,则是第一次进入
editor.putBoolean("isFirstIn", false);
//将欢迎页停留3秒,并且将message设置为跳转到
mhandler.sendEmptyMessageDelayed(GO_GUIDE,TIME);
}
//否则引导页SplashActivity,1秒后跳转在goGuide中实现Splash变量为时间
else {
mhandler.sendEmptyMessageDelayed(GO_MAIN,Splash);//将欢迎页停留2秒,并且将message设置文跳转到 MainActivity,跳转功能在goMain中实现
}
editor.commit();
}
}
第二步创建一个GuideActivity类用于ViewPager引导
package com.MrYu.Main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class GuideActivity extends Activity {
//自定义所需的控件
private ViewPager viewPager;//创建所需要的ViewPaeger
private PagerAdapter mAdapter;//创建所需要的PagerAdapter适配器
private List<View> mViews=new ArrayList<>();//准备数据源引入Jre1.7
private Button bt_home;//在ViewPager的最后一个页面设置一个按钮,用于点击跳转到MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//在AndroidManifest配置中添加注册GuideActivity方法
setContentView(R.layout.activity_guide);
initView();//调用InItView方法
}
private void initView() {
//创建一个ViewPager指定id<viewpager>
viewPager= (ViewPager) findViewById(R.id.view_pager);
//将每个xml文件inflater转化为View
LayoutInflater inflater=LayoutInflater.from(this);
//注册所需的XML
View guideOne=inflater.inflate(R.layout.activity_guide_one, null);//每个xml中就放置一个imageView
View guideTwo=inflater.inflate(R.layout.activity_guide_two,null);
View guideThree=inflater.inflate(R.layout.activity_guide_there,null);
View guideFive=inflater.inflate(R.layout.activity_guide_five,null);
//List集合
mViews.add(guideOne);//将view加入到list中
mViews.add(guideTwo);
mViews.add(guideThree);
mViews.add(guideFive);
//new一个PagerAdapter
mAdapter=new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view=mViews.get(position);//初始化适配器,将view加到container中
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view=mViews.get(position);
container.removeView(view);//将view从container中移除
}
@Override
public int getCount() {
return mViews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;//判断当前的view是我们需要的对象
}
};
viewPager.setAdapter(mAdapter);
//activity切换的控件id=to.Main
bt_home= (Button) guideFive.findViewById(R.id.to_Main);
bt_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent跳转
Intent intent=new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
//结束
finish();
}
});
}
}
创建n个xml用于引导页布局,我这里用了四页1-3都是一样的第四个不同的是有一个控件用于Activity切换
Drawable文件放最高分辨率下面
第一个XMLactivity_guide_one
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/viewgare_one"/>
</LinearLayout>
第二个activity_guide_two
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/viewgare_two"/>
</LinearLayout>
第三个activity_guide_there
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/viewgare_there"/>
</LinearLayout>
最后一个activity_guide_five
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/viewpager_five"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/to_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进入主页"/>
</LinearLayout>
</RelativeLayout>
这个xml是用于View.V4架包的调用view继承Viewpager.view.4
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bysjlwdx.welcome.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Splash的xml我就不写了
继续第三步就是MainActivity啦这个不用改,不懂得看我的
package com.MrYu.Main;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
接下来Android配置文件Java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MrYu.Main"
android:versionCode="1"
android:versionName="1.0" >
<!-- SDK最大适应最小值 21API-->
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<!-- 修改Theme改为所有Activity去掉标题栏图标 ,需要修改的就调用主题-->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.NoActionBar">
<activity
android:name="com.MrYu.Main.SplashActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 不使用默认的Theme,指定为全屏显示 -->
<activity
android:name="com.MrYu.Main.GuideActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity>
<!-- 使用默认的主题 -->
<activity
android:name="com.MrYu.Main.MainActivity"
android:theme="@android:style/Theme.Holo.Light.NoActionBar"></activity>
</application>
</manifest>