你好,游客 登录 注册 搜索
背景:
阅读新闻

Android ViewPager控件的使用【附源码】

[日期:2012-06-18] 来源:Linux社区  作者:ericchan2012 [字体:大 中 小]

然后主要是看代码里面怎么用的,viewpager需要一个adapter来适配里面的每个layout。

  1. public class ViewPageDemoActivity extends Activity {   
  2.     private ViewPager mPager;// 页卡内容   
  3.     private List<View> listViews; // Tab页面列表   
  4.     private ImageView cursor;// 动画图片   
  5.     private TextView t1, t2, t3;// 页卡头标   
  6.     private int offset = 0;// 动画图片偏移量   
  7.     private int currIndex = 0;// 当前页卡编号   
  8.     private int bmpW;// 动画图片宽度   
  9.     MyPagerAdapter adapter;   
  10.     LayoutInflater mInflater;   
  11.   
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.         Log.i("Viewpage","--onCreate--");   
  18.         initImageView();   
  19.         initTextView();   
  20.         initPageView();   
  21.            
  22.     }   
  23.   
  24.     private void initPageView() {   
  25.         mInflater = getLayoutInflater();   
  26.         listViews = new ArrayList<View>();   
  27.         listViews.add(mInflater.inflate(R.layout.item1, null));   
  28.         listViews.add(mInflater.inflate(R.layout.item2, null));   
  29.         listViews.add(mInflater.inflate(R.layout.item3, null));   
  30.         adapter = new MyPagerAdapter(listViews);   
  31.         mPager = (ViewPager) findViewById(R.id.vPager);   
  32.         mPager.setAdapter(adapter);   
  33.         mPager.setCurrentItem(0);   
  34.         mPager.setOnPageChangeListener(new MyOnPageChangeListener());   
  35.     }   
  36.   
  37.     private void initTextView() {   
  38.         t1 = (TextView) findViewById(R.id.text1);   
  39.         t2 = (TextView) findViewById(R.id.text2);   
  40.         t3 = (TextView) findViewById(R.id.text3);   
  41.         t1.setOnClickListener(new MyOnClickListener(0));   
  42.         t2.setOnClickListener(new MyOnClickListener(1));   
  43.         t3.setOnClickListener(new MyOnClickListener(2));   
  44.     }   
  45.   
  46.     private void initImageView() {   
  47.         cursor = (ImageView) findViewById(R.id.cursor);   
  48.         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.slide)   
  49.                 .getWidth();// 获取图片宽度   
  50.         DisplayMetrics dm = new DisplayMetrics();   
  51.         getWindowManager().getDefaultDisplay().getMetrics(dm);   
  52.         int screenW = dm.widthPixels;// 获取分辨率宽度   
  53.         offset = (screenW / 3 - bmpW) / 2;// 计算偏移量   
  54.         Matrix matrix = new Matrix();   
  55.         matrix.postTranslate(offset, 0);   
  56.         cursor.setImageMatrix(matrix);// 设置动画初始位置   
  57.     }   
  58.   
  59.     private class MyOnClickListener implements View.OnClickListener {   
  60.         private int index = 0;   
  61.   
  62.         public MyOnClickListener(int i) {   
  63.             index = i;   
  64.         }   
  65.   
  66.         @Override  
  67.         public void onClick(View v) {   
  68.             // TODO Auto-generated method stub   
  69.             mPager.setCurrentItem(index);   
  70.         }   
  71.     }   
  72.   
  73.     public class MyPagerAdapter extends PagerAdapter {   
  74.         public List<View> mListViews;   
  75.   
  76.         public MyPagerAdapter(List<View> mListViews) {   
  77.             this.mListViews = mListViews;   
  78.         }   
  79.   
  80.         public void destroyItem(View arg0, int arg1, Object arg2) {   
  81.             ((ViewPager) arg0).removeView(mListViews.get(arg1));   
  82.         }   
  83.   
  84.         public void finishUpdate(View arg0) {   
  85.         }   
  86.   
  87.         @Override  
  88.         public int getCount() {   
  89.             return mListViews.size();   
  90.         }   
  91.   
  92.         @Override  
  93.         public Object instantiateItem(View arg0, int arg1) {   
  94.             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);   
  95.             return mListViews.get(arg1);   
  96.         }   
  97.   
  98.         @Override  
  99.         public boolean isViewFromObject(View arg0, Object arg1) {   
  100.             return arg0 == (arg1);   
  101.         }   
  102.   
  103.         @Override  
  104.         public void restoreState(Parcelable arg0, ClassLoader arg1) {   
  105.         }   
  106.   
  107.         @Override  
  108.         public Parcelable saveState() {   
  109.             return null;   
  110.         }   
  111.   
  112.         @Override  
  113.         public void startUpdate(View arg0) {   
  114.         }   
  115.     }   
  116.   
  117.     public class MyOnPageChangeListener implements OnPageChangeListener {   
  118.   
  119.         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量   
  120.         int two = one * 2;// 页卡1 -> 页卡3 偏移量   
  121.   
  122.         @Override  
  123.         public void onPageSelected(int arg0) {   
  124.             Animation animation = null;   
  125.             switch (arg0) {   
  126.             case 0:   
  127.                 if (currIndex == 1) {   
  128.                     animation = new TranslateAnimation(one, 0, 0, 0);   
  129.                 } else if (currIndex == 2) {   
  130.                     animation = new TranslateAnimation(two, 0, 0, 0);   
  131.                 }   
  132.                 break;   
  133.             case 1:   
  134.                 if (currIndex == 0) {   
  135.                     animation = new TranslateAnimation(offset, one, 0, 0);   
  136.                 } else if (currIndex == 2) {   
  137.                     animation = new TranslateAnimation(two, one, 0, 0);   
  138.                 }   
  139.                 break;   
  140.             case 2:   
  141.                 if (currIndex == 0) {   
  142.                     animation = new TranslateAnimation(offset, two, 0, 0);   
  143.                 } else if (currIndex == 1) {   
  144.                     animation = new TranslateAnimation(one, two, 0, 0);   
  145.                 }   
  146.                 break;   
  147.             }   
  148.             currIndex = arg0;   
  149.             animation.setFillAfter(true);// True:图片停在动画结束位置   
  150.             animation.setDuration(300);   
  151.             cursor.startAnimation(animation);   
  152.         }   
  153.   
  154.         @Override  
  155.         public void onPageScrolled(int arg0, float arg1, int arg2) {   
  156.         }   
  157.   
  158.         @Override  
  159.         public void onPageScrollStateChanged(int arg0) {   
  160.         }   
  161.     }   
  162. }  

当然,你需要为每个tab对应一个layout,就想上面的item1,item2,item3一样。

这样就能实现左右滑动的一个效果了。

顺便说一下,viewpager刚开始推出,还没加入到正式的sdk,需要一个额外的jar包,运行工程的时候导入一下jar包就好了。

源码见附件。

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/6月/18日/Android ViewPager控件的使用/

linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* 匿名 发表于 2012/11/14 15:24:38
坑人啊!没有
第 1 楼
* , 发表于 2012/7/12 15:47:43
,