與閃屏界面同用一個(gè)基類base和一個(gè)APP類
看閃屏界面的
Activity中
public class MainActivity extends Base {
private MainPagerAdapter adapter;
private ViewPager mPager;
@Override //設(shè)置全局
protected void init() {
//全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
//判斷啟動(dòng)的界面
SharedPreferences sp=getSharedPreferences("appstar",MODE_PRIVATE);
//用于記錄是否第一次啟動(dòng) isFirst
boolean isFirst=sp.getBoolean("isFirst",true);
Intent intent = getIntent();
boolean help=intent.getBooleanExtra("help",false);
if (isFirst || help){
//記錄狀態(tài)佃却,表示啟動(dòng)過
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) {
//判斷停留在最后一張的時(shí)間為2秒败玉,否則不跳轉(zhuǎn)
//如果想停留幾秒后跳轉(zhuǎn)則不加if
if (position==adapter.getCount()-1){
timer = new Timer(false);
//計(jì)數(shù)器開始運(yùn)行
timer.schedule(new TimerTask() {
@Override
public void run() {
openActivity(ShanActivity.class,true); //如果時(shí)間夠了則跳轉(zhuǎn)领迈,并且關(guān)閉上一個(gè)Acyivity
}
},2000);
}else {
if (timer!=null)
timer.cancel();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override //設(shè)置適配器
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的數(shù)量
* @return
*/
@Override
public int getCount() {
return item.length;
}
/**
* 判斷是否創(chuàng)建新的
* @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;
}
/**
* 是否刪除舊的視圖规丽,默認(rèn)為3個(gè)布局
* @param container
* @param position
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
avtivity的布局份企,也是橫屏滑動(dòng)
<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>