首先ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包。大家没有的话去网上随便下个就好。
首先我们先创建一个页面来判断该应用是否是第一次进入,是的话跳转到viewpager引导页,不是的话进入主界面
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class GuideActivity extends Activity{
private final int HOME=100;//主界面
private final int GUIDE=101;//引导界面
private boolean isFirstIn=false;
private int TIME=1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// 首先先创建一个欢迎进入的布局
RelativeLayout layout=new RelativeLayout(this);
ImageView imageview=new ImageView(this);
LayoutParams layoutParams=new
LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageview.setImageResource(R.drawable.splash);
imageview.setLayoutParams(layoutParams);
layout.addView(imageview);
setContentView(layout);
init();
super.onCreate(savedInstanceState);
}
private void init() {
// 存储数据
SharedPreferences sharedPreferences=getSharedPreferences("Android",MODE_PRIVATE);
isFirstIn=sharedPreferences.getBoolean("isfirst", true);
//判断
if (!isFirstIn) {
handler.sendEmptyMessageDelayed(HOME,TIME);//给新线程发送一个指令让他进入home界面
}else{
handler.sendEmptyMessageDelayed(GUIDE, TIME);//给新线程发送一个指令让他进入GUIDE界面
Editor editor=sharedPreferences.edit();//修改
editor.putBoolean("isfirst", false);
editor.commit();//提交
}
}
//新线程
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case HOME:
Intent intent=new Intent(GuideActivity.this, ZhuActivity.class);
startActivity(intent);
finish();
break;
case GUIDE:
Intent intent1=new Intent(GuideActivity.this, YindaoActivity.class);
startActivity(intent1);
finish();
break;
}
};
};
}
import java.util.ArrayList;
import java.util.List;
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.View;
import android.view.View.OnClickListener;
public class YindaoActivity extends Activity {
private ViewPager viewPager;
private List<View> alist;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yindao);
viewPager=(ViewPager) findViewById(R.id.viewpager);
alist=new ArrayList<View>();
//布局解析器
View view1=View.inflate(this, R.layout.fragment1, null);
View view2=View.inflate(this, R.layout.fragment2, null);
View view3=View.inflate(this, R.layout.fragment3, null);
alist.add(view1);
alist.add(view2);
alist.add(view3);
viewPager.setAdapter(adapter);
//点击事件
view3.findViewById(R.id.start).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(YindaoActivity.this, ZhuActivity.class));
finish();
}
});
}
PagerAdapter adapter=new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return alist.size();
}
// 销毁视图
public void destroyItem(android.view.ViewGroup container, int position,
Object object) {
container.removeView(alist.get(position));
};
// 重新加载视图
public Object instantiateItem(android.view.ViewGroup container,
int position) {
container.addView(alist.get(position));
return alist.get(position);
};
};
}
主界面的话这里就不写了,大家根据需求自己去写
还有大家记得把首次进入设置成GuideActivity.
下面是fragment3的布局,其他两个也差不多,就不写了
<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"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:scaleType="fitXY"
android:src="@drawable/tutorial_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/new_feature_bt"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="17dp"
/>
</RelativeLayout>