ViewPager是谷歌官方提供的兼容低版本安卓设备的软件包,里面包含了只有在安卓3.0以上可以使用的api。Viewpager现在也算是标配了,如果一个App没有用到ViewPager感觉还是比较罕见的,导航和页面菜单常用的功能,ViewPager与LisstView类似,ListView经常会用到BaseAdapter,ViewPager则继承的是PagerAdapter,关于简单的使用可以去官网可以http://developer.android.com/reference/android/support/v4/view/ViewPager.html当做参考了解一下,开始正题吧:
看下效果图吧,网上好多都是放美女的,我还是简单点就放个TextView吧:
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.googleviewpager.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </RelativeLayout>
需要切换的三个布局文件,one.xml,two.xml,three.xml,one.xml的代码,其他两个类似就不贴代码了:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:text="周永康被开" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
onCreate中的代码,其实分别加载了三个View,其中第一个和最后一个加载了一个空的View,主要是为了能够左右循环:
setContentView(R.layout.activity_main); LayoutInflater inflater=getLayoutInflater(); viewPager=(ViewPager) findViewById(R.id.viewpager); view1=inflater.inflate(R.layout.one,null); view1.setBackgroundColor(Color.RED); view2=inflater.inflate(R.layout.two,null); view2.setBackgroundColor(Color.BLACK); view3=inflater.inflate(R.layout.three,null); view3.setBackgroundColor(Color.BLUE); viewList=new ArrayList<View>(); viewList.add(new View(this)); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(new View(this)); viewPager.setAdapter(new MyViewPagerAdapter(viewList)); viewPager.setCurrentItem(1);
自定义的MyViewPagerAdapter:
private class MyViewPagerAdapter extends PagerAdapter { @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(myList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(myList.get(position), 0); return myList.get(position); } private List<View> myList; public MyViewPagerAdapter(List<View> list) { myList=list; } @Override public int getCount() { // TODO Auto-generated method stub return myList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==(arg1); } }
上面写完之后还是需要设置一下页面setOnPageChangeListener的事件,循环的重点就是切换到一个空的View时将页面替换掉就可以:
viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub System.out.println("onPageSelected = " + arg0); if(arg0 == 0) viewPager.setCurrentItem(viewList.size()-2); else if(arg0 == viewList.size() - 1) viewPager.setCurrentItem(1); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } });
最后看一张切换中的效果吧:
Demo比较简单,不过对于入门应该是足够了~