Kenan
Assistant Engineer
Assistant Engineer
  • UID621
  • Fans0
  • Follows0
  • Posts55
Reads:817Replies:0

Implement Gallery effect with ViewPager, with big picture shown in the middle and small pictures sho

Created#
More Posted time:Nov 23, 2016 9:46 AM
Previously there was a Gallery project but it had spaghetti code. Don’t ask me why as I also just took over the project. To facilitate future reading and maintenance, I refactored some modules. At first, some simple effects are achieved, but at the very beginning, sliding left had some problems. This is because for the getCount of our adapter, we want to achieve an infinite loop through Integer.MAX_VALUE. Simply put, this is rightward (infinitely large), so only the rightward loop is achieved, and the leftward loop is still not working.
So how can we solve this problem?
First, we still follow the previous writing method for the adapter. Actually the adapter helps us to process the interface, and it is better not to add other logic. My previous thought is to use
viewPager.addOnPageChangeListener to achieve this and realize loop sliding by judging whether it is slid to the leftmost or rightmost side.
int realPosition = mAdapter.toRealPosition(position);  
               if (mPreviousPosition != realPosition) {  
                   mPreviousPosition = realPosition;  
                   if (mOuterPageChangeListener != null) {  
                       mOuterPageChangeListener.onPageSelected(realPosition);  
                   }  
               }


Here I encapsulate a class. You only need to change your ViewPager to the ViewPager I encapsulated.
LoopViewPager
public class LoopViewPager extends ViewPager {  
      
        private static final boolean DEFAULT_BOUNDARY_CASHING = false;  
      
        private OnPageChangeListener mOuterPageChangeListener;  
        private LoopPagerAdapterWrapper mAdapter;  
        private boolean mBoundaryCaching = DEFAULT_BOUNDARY_CASHING;  
      
        public static int toRealPosition( int position, int count ){  
            position = position-1;  
            if( position < 0 ){  
                position += count;  
            }else{  
                position = position%count;  
            }  
            return position;  
        }  
          
        public void setBoundaryCaching(boolean flag) {  
            mBoundaryCaching = flag;  
            if (mAdapter != null) {  
                mAdapter.setBoundaryCaching(flag);  
            }  
        }  
      
        @Override  
        public void setAdapter(PagerAdapter adapter) {  
            mAdapter = new LoopPagerAdapterWrapper(adapter);  
            mAdapter.setBoundaryCaching(mBoundaryCaching);  
            super.setAdapter(mAdapter);  
            setCurrentItem(0, false);  
        }  
      
        @Override  
        public PagerAdapter getAdapter() {  
            return mAdapter != null ? mAdapter.getRealAdapter() : mAdapter;  
        }  
      
        @Override  
        public int getCurrentItem() {  
            return mAdapter != null ? mAdapter.toRealPosition(super.getCurrentItem()) : 0;  
        }  
      
        public void setCurrentItem(int item, boolean smoothScroll) {  
            int realItem = mAdapter.toInnerPosition(item);  
            super.setCurrentItem(realItem, smoothScroll);  
        }  
      
        @Override  
        public void setCurrentItem(int item) {  
            if (getCurrentItem() != item) {  
                setCurrentItem(item, true);  
            }  
        }  
      
        @Override  
        public void setOnPageChangeListener(OnPageChangeListener listener) {  
            mOuterPageChangeListener = listener;  
        };  
      
        public LoopViewPager(Context context) {  
            super(context);  
            init();  
        }  
      
        public LoopViewPager(Context context, AttributeSet attrs) {  
            super(context, attrs);  
            init();  
        }  
      
        private void init() {  
            super.setOnPageChangeListener(onPageChangeListener);  
        }  
      
        private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {  
            private float mPreviousOffset = -1;  
            private float mPreviousPosition = -1;  
      
            @Override  
            public void onPageSelected(int position) {  
      
                int realPosition = mAdapter.toRealPosition(position);  
                if (mPreviousPosition != realPosition) {  
                    mPreviousPosition = realPosition;  
                    if (mOuterPageChangeListener != null) {  
                        mOuterPageChangeListener.onPageSelected(realPosition);  
                    }  
                }  
            }  
      
            @Override  
            public void onPageScrolled(int position, float positionOffset,  
                    int positionOffsetPixels) {  
                int realPosition = position
Guest