引用:http://www.apkbus.com/android-18384-1-1.html

为ViewFlipper视图切换增加动画Android中实现视图随手势移动中实现了视图随手势切换,现在Android中Compatibility Package提供了ViewPager可以更简便的实现视图切换,实现的效果如下:

<ignore_js_op>

效果和ViewGroup一样,但是实现过程更简单.新版的Android Market和Google+都是用了ViewPager.

<ignore_js_op>

说一下实现过程:

工程目录如下:

<ignore_js_op>

MyPagerActivity的onCreate方法如下:

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. initPageContent();
  6. awesomeAdapter = new MyPagerAdapter();
  7. awesomePager = (ViewPager) findViewById(R.id.awesomepager);
  8. awesomePager.setAdapter(awesomeAdapter);
  9. }

复制代码

其中main.xml布局文件引入了ViewPager:

  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#a4c639">
  6. <android.support.v4.view.viewpager
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:id="@+id/awesomepager"/>

复制代码

MyPagerAdapter继承了PagerAdapter:

  1. private class MyPagerAdapter extends PagerAdapter{
  2. @Override
  3. public int getCount() {
  4. return imageS.length;
  5. }
  6. /**
  7. * Create the page for the given position.  The adapter is responsible
  8. * for adding the view to the container given here, although it only
  9. * must ensure this is done by the time it returns from
  10. * {@link #finishUpdate()}.
  11. *
  12. * @param container The containing View in which the page will be shown.
  13. * @param position The page position to be instantiated.
  14. * @return Returns an Object representing the new page.  This does not
  15. * need to be a View, but can be some other container of the page.
  16. */
  17. @Override
  18. public Object instantiateItem(View collection, int position) {
  19. View view = getLayoutInflater().inflate(R.layout.page,null);
  20. ImageView imageView =(ImageView) view.findViewById(R.id.imageId);
  21. imageView.setImageDrawable(imageS[position]);
  22. ((ViewPager) collection).addView(view,0);
  23. return view;
  24. }
  25. /**
  26. * Remove a page for the given position.  The adapter is responsible
  27. * for removing the view from its container, although it only must ensure
  28. * this is done by the time it returns from {@link #finishUpdate()}.
  29. *
  30. * @param container The containing View from which the page will be removed.
  31. * @param position The page position to be removed.
  32. * @param object The same object that was returned by
  33. * {@link #instantiateItem(View, int)}.
  34. */
  35. @Override
  36. public void destroyItem(View collection, int position, Object view) {
  37. ((ViewPager) collection).removeView((View) view);
  38. }
  39. @Override
  40. public boolean isViewFromObject(View view, Object object) {
  41. return view==((View)object);
  42. }
  43. /**
  44. * Called when the a change in the shown pages has been completed.  At this
  45. * point you must ensure that all of the pages have actually been added or
  46. * removed from the container as appropriate.
  47. * @param container The containing View which is displaying this adapter’s
  48. * page views.
  49. */
  50. @Override
  51. public void finishUpdate(View arg0) {}
  52. @Override
  53. public void restoreState(Parcelable arg0, ClassLoader arg1) {}
  54. @Override
  55. public Parcelable saveState() {
  56. return null;
  57. }
  58. @Override
  59. public void startUpdate(View arg0) {}
  60. }

复制代码

其中红色代码部分负责加载Layout和想layout中填充View.这样就实现了视图的随手势切换.源代码见: <ignore_js_op> android-viewpager0.1.rar (375.4 KB, 下载次数: 813)

最新文章

  1. mybatis中的延迟加载
  2. CentOS 6 安装 python 2.7 和 mod_wsgi 运行pyocr[tesseract]
  3. IT技术的进化道路
  4. Linux运维操作
  5. SQL 基本的函数
  6. php include 与 require 起底[转]
  7. mysql 分库分表的方法
  8. ORACLE单字符函数的函数
  9. 使用JavaScript重定向URL参数
  10. Spark Core_资源调度与任务调度详述
  11. c# winform中的一段代码赏析
  12. Mlecms 反射型xss &amp;&amp; 后台任意文件下载
  13. JDK源码分析(四)—— ConcurrentHashMap
  14. vue 数字随机滚动(数字递增)
  15. mysql练习----More JOIN operations
  16. docker之docker-machine用法
  17. October 20th 2017 Week 42nd Friday
  18. [LeetCode] Meeting Rooms I &amp; II
  19. Jolokia
  20. javascript 的继承实例

热门文章

  1. [转载]TableView详解
  2. 在不知道json格式的情况下如何使用cjson进行解析
  3. php by oneself
  4. Jenkins 2.7.3 LTS 发布
  5. css设置移动端checkbox和radio样式
  6. [LintCode] Candy 分糖果问题
  7. 安装repcached,并且测试其双向复制是否成功
  8. C++ STL
  9. 一道打印M的面试题
  10. itertools 介绍