viewpaper 抽屉

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

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

<ignore_js_op>

viewpaper 抽屉

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

<ignore_js_op>

viewpaper 抽屉

说一下实现过程:

工程目录如下:

<ignore_js_op>

viewpaper 抽屉

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>viewpaper 抽屉 android-viewpager0.1.rar (375.4 KB, 下载次数: 813)

上一篇:100个Github上Android开源库


下一篇:Python 3 利用 Dlib 19.7 实现摄像头人脸识别