与闪屏界面同用一个基类base和一个APP类
看闪屏界面的
Activity中
public class MainActivity extends Base {
private MainPagerAdapter adapter;
private ViewPager mPager;
@Override //设置全局
protected void init() {
//全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
//判断启动的界面
SharedPreferences sp=getSharedPreferences("appstar",MODE_PRIVATE);
//用于记录是否第一次启动 isFirst
boolean isFirst=sp.getBoolean("isFirst",true);
Intent intent = getIntent();
boolean help=intent.getBooleanExtra("help",false);
if (isFirst || help){
//记录状态,表示启动过
sp.edit().putBoolean("isFirst",false).commit();
}else {
openActivity(ShanActivity.class,true);
}
}
@Override
protected void setListaner() {
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
private Timer timer;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//判断停留在最后一张的时间为2秒,否则不跳转
//如果想停留几秒后跳转则不加if
if (position==adapter.getCount()-1){
timer = new Timer(false);
//计数器开始运行
timer.schedule(new TimerTask() {
@Override
public void run() {
openActivity(ShanActivity.class,true); //如果时间够了则跳转,并且关闭上一个Acyivity
}
},2000);
}else {
if (timer!=null)
timer.cancel();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override //设置适配器
protected void data() {
adapter = new MainPagerAdapter();
mPager.setAdapter(adapter);
}
@Override
protected void view() {
mPager=getViewById(R.id.vpa_main);
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected boolean showTitle() {
return false;
}
}
适配器
public class MainPagerAdapter extends PagerAdapter {
//图片的集合
int[] item={R.mipmap.item1,R.mipmap.item2,R.mipmap.item3,R.mipmap.item4};
/**
* item的数量
* @return
*/
@Override
public int getCount() {
return item.length;
}
/**
* 判断是否创建新的
* @param view
* @param object
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
/**
* 图片的更换
* @param container
* @param position
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view=LayoutInflater.from(container.getContext()).inflate(R.layout.item_main,null);
ImageView imageView= (ImageView) view.findViewById(R.id.img_main);
imageView.setBackgroundResource(item[position]);
container.addView(view);
return view;
}
/**
* 是否删除旧的视图,默认为3个布局
* @param container
* @param position
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
avtivity的布局,也是横屏滑动
<LinearLayout 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.bf.com.myapplication.Activity.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vpa_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
网友评论