当前位置:   article > 正文

Android Viewpager+Fragment 实现画廊式效果_android viewpager+fragment 画廊效果

android viewpager+fragment 画廊效果

先看一下效果图

3 1  2  3 1

向左滑动到最后一个 显示出第一个

向右滑动到第一个 显示出最后一个

1.创建一个自定义类 ZoomOutPageTransformer (实现放大缩小)

  1. public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
  2. //自由控制缩放比例
  3. private static final float MAX_SCALE = 1f;
  4. private static final float MIN_SCALE = 0.8f;//0.85f
  5. @Override
  6. public void transformPage(View page, float position) {
  7. if (position <= 1) {
  8. float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
  9. page.setScaleX(scaleFactor);
  10. if (position > 0) {
  11. page.setTranslationX(-scaleFactor * 2);
  12. } else if (position < 0) {
  13. page.setTranslationX(scaleFactor * 2);
  14. }
  15. page.setScaleY(scaleFactor);
  16. } else {
  17. page.setScaleX(MIN_SCALE);
  18. page.setScaleY(MIN_SCALE);
  19. }
  20. }
  21. }

2.activity_main布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/ll_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:clipChildren="false"
  7. android:orientation="vertical">
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:gravity="center"
  12. android:padding="5dp"
  13. android:text="viewpager实现画廊效果"
  14. android:textColor="@color/colorPrimary"/>
  15. <androidx.viewpager.widget.ViewPager
  16. android:id="@+id/viewPager"
  17. android:layout_width="200dp"
  18. android:layout_height="300dp"
  19. android:layout_gravity="center"
  20. android:layout_marginBottom="30dp"
  21. android:clipChildren="false"
  22. android:layout_marginTop="30dp"/>
  23. </LinearLayout>

3.Activity_main

  1. public class MainActivity extends AppCompatActivity {
  2. private ViewPager mViewPager;
  3. private LinearLayout ll_layout;
  4. private List<Fragment> list;
  5. private boolean mIsChanged = false;
  6. private int mCurrentPagePosition = FIRST_ITEM_INDEX;
  7. private static final int POINT_LENGTH = 3;
  8. private static final int FIRST_ITEM_INDEX = 1;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. initView();
  14. }
  15. private void initView() {
  16. mViewPager = (ViewPager) findViewById(R.id.viewPager);
  17. ll_layout = (LinearLayout) findViewById(R.id.ll_layout);
  18. //添加到数组
  19. list =new ArrayList<>();
  20. list.add(new Fragment_three());
  21. list.add(new Fragment_one());
  22. list.add(new Fragment_two());
  23. list.add(new Fragment_three());
  24. list.add(new Fragment_one());
  25. //设置适配器
  26. mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
  27. @Override
  28. public Fragment getItem(int position) {
  29. return list.get(position);
  30. }
  31. @Override
  32. public int getCount() {
  33. return list.size();
  34. }
  35. });
  36. mViewPager.setCurrentItem(1);
  37. mViewPager.setPageMargin(20);
  38. mViewPager.setOffscreenPageLimit(list.size());
  39. mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());//设置画廊模式
  40. mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  41. @Override
  42. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  43. }
  44. @Override
  45. public void onPageSelected(int position) {
  46. mIsChanged = true;
  47. if (position > POINT_LENGTH) {
  48. // 末位之后,跳转到首位(1)
  49. mCurrentPagePosition = FIRST_ITEM_INDEX;
  50. } else if (position < FIRST_ITEM_INDEX) {
  51. // 首位之前,跳转到末尾(N)
  52. mCurrentPagePosition = POINT_LENGTH;
  53. } else {
  54. mCurrentPagePosition = position;
  55. }
  56. }
  57. @Override
  58. public void onPageScrollStateChanged(int state) {
  59. if (ViewPager.SCROLL_STATE_IDLE == state) {
  60. if (mIsChanged) {
  61. mIsChanged = false;
  62. mViewPager.setCurrentItem(mCurrentPagePosition, false);
  63. }
  64. }
  65. }
  66. });
  67. // //viewPager左右两边滑动无效的处理
  68. // ll_layout.setOnTouchListener(new View.OnTouchListener() {
  69. // @Override
  70. // public boolean onTouch(View view, MotionEvent motionEvent) {
  71. // return mViewPager.dispatchTouchEvent(motionEvent);
  72. // }
  73. // });
  74. }
  75. }

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号