标签:android
案例十:启动页
作用:
1. 打广告,放几张有点创意或者好看的图片吸引眼球
2. 数据准备,预加载一些数据,创建表什么的
知识点:
涉及文件:
关于布局文件,这次主要引用的还是ImageView控件,用来展示效果,在此就不多做解释。
主要来看下ViewPager以及ViewPagerAdapter的使用
guideActivity.java
public class GuideActivity extends ActionBarActivity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views;
// 底部小点图片
private ImageView[] dots;
// 记录当前选中位置
private int currentIndex;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
initView();
initDots();
}
private void initView(){
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.what_new_one,null));
views.add(inflater.inflate(R.layout.what_new_tow,null));
views.add(inflater.inflate(R.layout.what_new_three,null));
views.add(inflater.inflate(R.layout.what_new_four,null));
//初始化Adapter
vpAdapter = new ViewPagerAdapter(views,this);
vp = (ViewPager)findViewById(R.id.guidePages);
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);
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter {
//存储数据的文件名
private static final String SHAREDPREFERENCES_NAME = "first_pref";
//界面列表
private List<View> views;
private Activity activity;
public ViewPagerAdapter(List<View> views,Activity activity){
this.views = views;
this.activity = activity;
}
// 销毁arg1位置的界面
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(views.get(arg1));
}
// 判断是否由对象生成界面
@Override
public boolean isViewFromObject(View view, Object parent) {
return (view == parent);
}
// 获得当前界面数
@Override
public int getCount() {
if (views != null) {
return views.size();
}
return 0;
}
@Override
public Object instantiateItem(View view,int postion){
((ViewPager)view).addView(views.get(postion),0);
if(postion == views.size()-1){
ImageView mStartImageButton = (ImageView)view.findViewById(R.id.iv_start);
mStartImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setGuide();
goHome();
}
});
}
return views.get(postion);
}
private void goHome(){
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
activity.finish();
}
private void setGuide(){
SharedPreferences preferences = activity.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("isFirstIn",false);
editor.commit();
}
}
当然还要考虑到全屏,AndroidManifest.xml文件中Activity的申明,不然最后的效果还是不怎么好看的。
其他几种实现的方式,大家可以了解了解
第一种: ViewFlipper + GestureDetector
第二种: ActivityGroup + GestureDetector
第三种: ViewFlow (开源项目)
标签:android
原文地址:http://blog.csdn.net/w324821962/article/details/44752587