首先,展示一下ViewPager是什么样子的,用过新浪微博客户端的应该对下面的画面很熟悉,(画面不是很美观,主要就是那么个意思,将就着看吧....)下面那个允许你来回滑动显示不同页面的区域就是一个ViewPager,在这里就不解释了.
布局文件如下:
activity_weibo.xml
<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" android:orientation="vertical"> <LinearLayout android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="40dp" android:background="#ffffff"> <TextView android:id="@+id/tt1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/tt1" android:gravity="center" android:textColor="#000000" android:textSize="20dp"/> <TextView android:id="@+id/tt2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/tt2" android:textSize="20dp" android:textColor="#000000"/> <TextView android:id="@+id/tt3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/tt3" android:textSize="20dp" android:textColor="#000000"/> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/image"/> <android.support.v4.view.ViewPager android:id="@+id/vPager" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:layout_gravity="center" android:background="#000000" android:flipInterval="30" android:persistentDrawingCache="animation" /> </LinearLayout>
lay1.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" > <View android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#66FF33"/> </LinearLayout>
lay2.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" > <View android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF3333"/> </LinearLayout>
lay3.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" > <View android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#3300FF"/> </LinearLayout>
下面是java代码:
package jason.viewpagerdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.Display; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class WeiboActivity extends Activity { private ViewPager viewPager;//可滑动的页卡内容,也就是我们主要练习的目标 private ImageView imageView;//类似游标的动画图片,这个就是那个左右滑动的小滑块 private TextView textView1,textView2,textView3; private List<View> views;//页面列表 private int offset = 0;//游标移动的偏移量 private int currentIndex = 0;//当前页面号码 private int bmpW;//游标宽度 private View view1,view2,view3;//各个页面卡片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weibo); InitImageView(); InitTextView(); InitViewPager(); } private void InitViewPager() { // TODO Auto-generated method stub viewPager = (ViewPager) findViewById(R.id.vPager); views = new ArrayList<View>(); LayoutInflater inflater = getLayoutInflater();//这个在我前面写的几篇里面有介绍Inflater view1 = inflater.inflate(R.layout.lay1, null); view2 = inflater.inflate(R.layout.lay2, null); view3 = inflater.inflate(R.layout.lay3, null); views.add(view1); views.add(view2); views.add(view3); viewPager.setAdapter(new MyViewPagerAdapter(views));//ViewPager跟ListView一样,也需要一个适配器,后面对PagerAdapter进行重写 viewPager.setCurrentItem(0);//默认显示第一个卡片页 viewPager.setOnPageChangeListener(new MyOnPageChangeListener());//给ViewPager加监听器 } private void InitTextView() { textView1 = (TextView) findViewById(R.id.tt1); textView2 = (TextView) findViewById(R.id.tt2); textView3 = (TextView) findViewById(R.id.tt3); textView1.setOnClickListener(new MyOnClickLis(0)); textView2.setOnClickListener(new MyOnClickLis(1)); textView3.setOnClickListener(new MyOnClickLis(2));//这些监听器保证在点击头部的标签时候页面也能滑动 } private void InitImageView() { imageView = (ImageView) findViewById(R.id.cursor); bmpW = imageView.getWidth(); DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int screen = displayMetrics.widthPixels; offset = (screen - bmpW *3)/6;//这个地方就是给每个标签左右各留出一块offset,共3个标签,6个offset, Matrix matrix = new Matrix();//后面我会再写一篇简要介绍Matrix的文章 matrix.postTranslate(offset, 0);这个就是把游标放在了第一个标签下面 imageView.setImageMatrix(matrix); } private class MyOnClickLis implements OnClickListener{ private int index; public MyOnClickLis(int index) {//注意,这里的监听器有一个默认的带参数的构造器,用来确定你点击的是哪一个标签 this.index = index; } @Override public void onClick(View v) { viewPager.setCurrentItem(index); } } //下面是比较重点的了,就是之前提到过的重写PagerAdapter,重写时候我们至少需要重写一下4个方法,当然这里我们也只写了这4个方法. public class MyViewPagerAdapter extends PagerAdapter{ private List<View> mListView; public MyViewPagerAdapter(List<View> views) { // TODO Auto-generated constructor stub this.mListView = views; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(mListView.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(mListView.get(position)); return mListView.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return mListView.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } } public class MyOnPageChangeListener implements OnPageChangeListener{ int one = offset *2 + bmpW;//卡片1 --> 卡片2 偏移量 int two = one * 2;//卡片2 --> 卡片3 偏移量 @Override public void onPageScrollStateChanged(int arg0) { //arg0 ==1的时候表示正在滑动,arg0==2的时候表示滑动完毕了,arg0==0的时候表示什么都没做,就是停在那。 } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { //表示在前一个页面滑动到后一个页面的时候,在前一个页面滑动前调用的方法 } @Override public void onPageSelected(int arg0) {//arg0是表示你当前选中的页面 Animation animation = new TranslateAnimation(one*currentIndex, one * arg0, 0, 0); currentIndex = arg0; animation.setFillAfter(true);//true:图片停留在动画结束的位置 animation.setDuration(300); imageView.startAnimation(animation); Toast.makeText(WeiboActivity.this, "卡片移向了第" + arg0 + "页", Toast.LENGTH_SHORT).show(); } } }