Change ViewPager animation duration when sliding programmatically

前端 未结 7 1086
清酒与你
清酒与你 2020-11-28 20:45

I\'m changing slide with the following code:

viewPager.setCurrentItem(index++, true);

But it changes too fast. Is there a way to set manual

相关标签:
7条回答
  • 2020-11-28 21:07

    I have found better solution, based on @df778899's answer and the Android ValueAnimator API. It works fine without reflection and is very flexible. Also there is no need for making custom ViewPager and putting it into android.support.v4.view package. Here is an example:

    private void animatePagerTransition(final boolean forward) {
    
        ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth());
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                viewPager.endFakeDrag();
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
                viewPager.endFakeDrag();
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    
            private int oldDragPosition = 0;
    
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int dragPosition = (Integer) animation.getAnimatedValue();
                int dragOffset = dragPosition - oldDragPosition;
                oldDragPosition = dragPosition;
                viewPager.fakeDragBy(dragOffset * (forward ? -1 : 1));
            }
        });
    
        animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS);
        if (viewPager.beginFakeDrag()) {
            animator.start();
        }
    }
    

    UPDATE:

    Just checked if this solution can be used to swipe several pages at once (for example if first page should be showed after the last one). This is slightly modified code to handle specified page count:

    private int oldDragPosition = 0;
    
    private void animatePagerTransition(final boolean forward, int pageCount) {
        // if previous animation have not finished we can get exception
        if (pagerAnimation != null) {
            pagerAnimation.cancel();
        }
        pagerAnimation = getPagerTransitionAnimation(forward, pageCount);
        if (viewPager.beginFakeDrag()) {    // checking that started drag correctly
            pagerAnimation.start();
        }
    }
    
    private Animator getPagerTransitionAnimation(final boolean forward, int pageCount) {
        ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - 1);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                viewPager.endFakeDrag();
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
                viewPager.endFakeDrag();
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
                viewPager.endFakeDrag();
                oldDragPosition = 0;
                viewPager.beginFakeDrag();
            }
        });
    
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int dragPosition = (Integer) animation.getAnimatedValue();
                int dragOffset = dragPosition - oldDragPosition;
                oldDragPosition = dragPosition;
                viewPager.fakeDragBy(dragOffset * (forward ? -1 : 1));
            }
        });
    
        animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS / pageCount); // remove divider if you want to make each transition have the same speed as single page transition
        animator.setRepeatCount(pageCount);
    
        return animator;
    }
    
    0 讨论(0)
提交回复
热议问题