转载

Android学习之ViewPager

1.定义

ViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view。其中,android.support.v4是谷歌公司为了解决当前版本碎片化的问题,从而提供的一个兼容的包。可以实现向下兼容的问题,让一些高版本的控件能够在低版本中进行使用;通过ViewPager可以实现左右翻页的效果。

2.实现思路

1.新建一个主Activity以及一个主View、四个其它View;

2.在那四个View的布局文件中添加TextView,设置名称为"视图1"、“视图2”....,并为每个视图设置了不同的背景色;

3.主View中添加ViewPager以及PagerTabStrip,并设置相关属性;

4.新建一个SnailPagerAdapter,他继承于PagerAdapter,并重写类中的方法;

5.在主Activity中初始化ViewPager、PagerAdapter,并通过setAdapter方法加载PagerAdapter。

3.具体实现

(1)代码结构图如下:

Android学习之ViewPager

(2)SnailPagerAdapter实现代码如下:

 1 package pager.snailtech.com.viewpagerdemo;  2   3 import android.support.v4.view.PagerAdapter;  4 import android.view.View;  5 import android.view.ViewGroup;  6 import java.util.List;  7   8   9 public class SnailPagerAdapter extends PagerAdapter { 10     private List<View> viewList; 11     private List<String> stringList; 12     public SnailPagerAdapter(List<View> viewList,List<String> stringList) 13     { 14         this.viewList=viewList; 15         this.stringList=stringList; 16     } 17  18     /** 19      * 获取页卡的数量 20      * @return 21      */ 22     @Override 23     public int getCount() { 24         return viewList.size(); 25     } 26  27     /** 28      *  判断当前的View是否来自于对象 29      * @param view 30      * @param object 31      * @return 32      */ 33     @Override 34     public boolean isViewFromObject(View view, Object object) { 35         return view==object; 36     } 37  38     /** 39      * 实例化一个页卡 40      */ 41  42     @Override 43     public Object instantiateItem(ViewGroup container, int position) { 44         container.addView(viewList.get(position)); 45         return viewList.get(position); 46     } 47  48     /** 49      * 销毁一个页卡 50      * @param container 51      * @param position 52      * @param object 53      */ 54     @Override 55     public void destroyItem(ViewGroup container, int position, Object object) { 56         container.removeView(viewList.get(position)); 57     } 58  59     /** 60      * 设置Viewpager页卡的标题 61      * @param position 62      * @return 63      */ 64     @Override 65     public CharSequence getPageTitle(int position) { 66         return stringList.get(position); 67     } 68 }

(3)主Activity的代码如下:

 1 package pager.snailtech.com.viewpagerdemo;  2   3 import android.support.v4.view.PagerTabStrip;  4 import android.support.v4.view.ViewPager;  5 import android.support.v7.app.ActionBarActivity;  6 import android.os.Bundle;  7 import android.view.Menu;  8 import android.view.MenuItem;  9 import android.view.View; 10  11 import java.util.ArrayList; 12 import java.util.List; 13  14  15 public class MainActivity extends ActionBarActivity { 16  17     private List<View> viewList; 18     private List<String> stringList; 19     private ViewPager viewPager; 20     private PagerTabStrip pagerTabStrip; 21     protected void onCreate(Bundle savedInstanceState) { 22         super.onCreate(savedInstanceState); 23         setContentView(R.layout.activity_main); 24         viewList=new ArrayList<View>(); 25         stringList=new ArrayList<String>(); 26         //获取view 27         View view1=View.inflate(this,R.layout.view1,null); 28         View view2=View.inflate(this,R.layout.view2,null); 29         View view3=View.inflate(this,R.layout.view3,null); 30         View view4=View.inflate(this,R.layout.view4,null); 31         //将view添加到viewList中 32         viewList.add(view1); 33         viewList.add(view2); 34         viewList.add(view3); 35         viewList.add(view4); 36         //添加页卡名称 37         stringList.add("第一页"); 38         stringList.add("第二页"); 39         stringList.add("第三页"); 40         stringList.add("第四页"); 41         //实例化pagerAdapter 42         SnailPagerAdapter pagerAdapter=new SnailPagerAdapter(viewList,stringList); 43         //获取ViewPager视图 44         viewPager=(ViewPager)findViewById(R.id.pager); 45  46         pagerTabStrip=(PagerTabStrip)findViewById(R.id.tab); 47         //pagerTabStrip.setDrawFullUnderline(false); 48  49  50         //加载pagerAdapter 51         viewPager.setAdapter(pagerAdapter); 52  53  54  55     } 56  57     @Override 58     public boolean onCreateOptionsMenu(Menu menu) { 59         // Inflate the menu; this adds items to the action bar if it is present. 60         getMenuInflater().inflate(R.menu.menu_main, menu); 61         return true; 62     } 63  64     @Override 65     public boolean onOptionsItemSelected(MenuItem item) { 66         // Handle action bar item clicks here. The action bar will 67         // automatically handle clicks on the Home/Up button, so long 68         // as you specify a parent activity in AndroidManifest.xml. 69         int id = item.getItemId(); 70  71         //noinspection SimplifiableIfStatement 72         if (id == R.id.action_settings) { 73             return true; 74         } 75  76         return super.onOptionsItemSelected(item); 77     } 78 }

(4)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=".MainActivity"  android:gravity="center">  <android.support.v4.view.ViewPager   android:id="@+id/pager"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_gravity="center">   <android.support.v4.view.PagerTabStrip    android:id="@+id/tab"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="top"    >   </android.support.v4.view.PagerTabStrip>  </android.support.v4.view.ViewPager> </RelativeLayout> 

4.效果截图

  Android学习之ViewPager

正文到此结束
Loading...