许多APP在第一次启动的时候,都会有welcome page。近日尝试利用ViewPager来实现Welcome Page。
d0711 完成记录,跟新下载地址
========================================================================
-
ViewPager简述
ViewPager可以实现多个页面滑动来回切换。其可以用作软件的欢迎界面,应用内广播页,或多图预览等。Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
android API http://www.android-doc.com/reference/android/support/v4/view/ViewPager.html -
ViewPager使用方法
-
Step1
准备好切换用的Layout布局文件(view类)
这里需要用LayoutInflater来定位。LayoutInflater与findViewById的区别见下。LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化
findViewById()是找xml布局文件下的具体widget控件 (如Button、TextView等) -
Step2
创建一个View类型的list,把需要添加的view放入该list。
上面两步的示例代码见下:views=new ArrayList<View>();
LayoutInflater inflater=getLayoutInflater();
view1=inflater.inflate(R.layout.welcome1, null);
view2=inflater.inflate(R.layout.welcome2, null);
view3=inflater.inflate(R.layout.welcome3, null);
views.add(view1);views.add(view2);views.add(view3); -
Step3
由于我们的ViewPager需要设置一个Adapter,故我们需要新建一个自己的Adapter类来继承PagerAdapter
这个类必须重写这四个函数,对于此具体的分析可见该博客:http://blog.csdn.net/harvic880925/article/details/38487149
下面是最简单的重写:public class MyViewPagerAdapter extends PagerAdapter{
private List<View> mListViews; public MyViewPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
} /*
* Remove a page for the given position.
* The adapter is responsible for removing the view from its container
* although it only must ensure this is done by the time it returns
* 该方法实现的功能是移除一个给定位置的页面
*
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mListViews.get(position));
} /*
* Create the page for the given position.
* The adapter is responsible for adding the view to the container given here,
* although it only must ensure this is done by the time it returns fromfinishUpdate(ViewGroup).
* 该方法实现的功能是添加一个给定位置的页面到容器
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mListViews.get(position), 0);
return mListViews.get(position);
} /*
* 获取view的数量
*/
@Override
public int getCount() {
return mListViews.size();
} /*
* Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int).
* This method is required for a PagerAdapter to function properly.
*
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
} -
Step4
实例化ViewPager类,并将views通过MyViewPagerAdapter来设置为ViewPager的Adapter。同时设置当前页,同时可以设置页面转换的监听器viewPager=(ViewPager) findViewById(R.id.vPager);
viewPager.setAdapter(new MyViewPagerAdapter(views));
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener(){ public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub } public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub } public void onPageSelected(int arg0) {
// TODO Auto-generated method stub } }); ViewPager性能问题
在实际使用ViewPager的时候,发现若采用1080p的素材,大部分测试手机均会出现明显的卡顿和掉针,用户体验很不好。网上有帖子采用延迟加载、优先缓存来解决这个问题。后来换成720p素材,大部分测试机均不会出现卡顿。当为3页时,测试450p为最佳(450*800)。- ViewPager简单实例
http://download.csdn.net/detail/he11o_liu/9572651
-