美文网首页
如何只实现addOnPageChangeListener的一个或

如何只实现addOnPageChangeListener的一个或

作者: FynnJason | 来源:发表于2018-09-07 17:56 被阅读201次

    前言

    我们知道,监听ViewPager滑动,需要通过addOnPageChangeListener来监听,addOnPageChangeListener需要实现以下3个方法:

    /**
         * Callback interface for responding to changing state of the selected page.
         */
        public interface OnPageChangeListener {
    
            /**
             * This method will be invoked when the current page is scrolled, either as part
             * of a programmatically initiated smooth scroll or a user initiated touch scroll.
             *
             * @param position Position index of the first page currently being displayed.
             *                 Page position+1 will be visible if positionOffset is nonzero.
             * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
             * @param positionOffsetPixels Value in pixels indicating the offset from position.
             */
            void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
    
            /**
             * This method will be invoked when a new page becomes selected. Animation is not
             * necessarily complete.
             *
             * @param position Position index of the new selected page.
             */
            void onPageSelected(int position);
    
            /**
             * Called when the scroll state changes. Useful for discovering when the user
             * begins dragging, when the pager is automatically settling to the current page,
             * or when it is fully stopped/idle.
             *
             * @param state The new scroll state.
             * @see ViewPager#SCROLL_STATE_IDLE
             * @see ViewPager#SCROLL_STATE_DRAGGING
             * @see ViewPager#SCROLL_STATE_SETTLING
             */
            void onPageScrollStateChanged(int state);
        }
    

    在实现开发中,很多时候我们并不需要都实现,为了减少代码量,那怎么才能只实现其中一个方法或者实现其中两个方法呢?

    方式

    通过源码,我们可以找到以下代码:

    /**
         * Simple implementation of the {@link OnPageChangeListener} interface with stub
         * implementations of each method. Extend this if you do not intend to override
         * every method of {@link OnPageChangeListener}.
         */
        public static class SimpleOnPageChangeListener implements OnPageChangeListener {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // This space for rent
            }
    
            @Override
            public void onPageSelected(int position) {
                // This space for rent
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
                // This space for rent
            }
        }
    

    如同注释所说,当你不需要实现所有方法时,就是用SimpleOnPageChangeListener,在代码中只需要这样调用即可:

    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                   
                }
            });
    

    虽然文章很短,但如果文章对你有帮助或者心头有了“又学到了”的感慨,希望点个喜欢支持一下,谢谢。

    相关文章

      网友评论

          本文标题:如何只实现addOnPageChangeListener的一个或

          本文链接:https://www.haomeiwen.com/subject/hkzcgftx.html