引用: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)

Android

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. [Android实例] android中进行https连接的方式的详解 (转发)
  3. Flutter ------- WebView加载网页
  4. 时间和日期选择器DatePicker和TimePicker的使用
  5. android 4.0系统webview加载网页出现白屏
  6. Android(安卓)中文API (46) —— SimpleAdapter
  7. Android(安卓)4.0 HDMI相关代码
  8. Android(安卓)编译过程分析(1)
  9. SystemBarTint的使用

随机推荐

  1. Android系统工具之Roblectric 使用过程中
  2. Android按返回键退出程序但不销毁
  3. Android运行报错:Error: Static interface
  4. 关于资源文件:颜色
  5. android中的广播接收器
  6. 解决Error:Android(安卓)Dex: com.androi
  7. android的软键盘的enter键的替换
  8. std::to_string is not memember of std
  9. Android打印工具类
  10. android官网demo之异步加载图片