Android Viewpager bounce to half a page(Android Viewpager 跳到半页)
问题描述
所以我想要实现的是用户将打开到视图寻呼机的第一页,而视图寻呼机将弹回第二页的一半并弹回第一页,表明还有更多页面要滚动到.我想知道如何实现这一点?
So what i am trying to achieve is user would open to first page of the view pager, and the view pager would bounce to half of the second page and bounce back to the fist page indicating that there are more pages to scroll to. I was wondering on how i could implement this?
推荐答案
你可以使用 fakeDragBy 方法来实现这个效果:
You can use fakeDragBy method to achieve this effect:
viewPager.beginFakeDrag();
viewPager.fakeDragBy(offset); //offset in pixels. 
viewPager.endFakeDrag();
我已经为此制定了方法:
I have made method for this:
private int animFactor;
private ValueAnimator animator = new ValueAnimator();
private void animateViewPager(final ViewPager pager, final int offset, final int delay) {
    if (!animator.isRunning()) {
        animator.removeAllUpdateListeners();
        animator.removeAllListeners();
        //Set animation
        animator.setIntValues(0, -offset);
        animator.setDuration(delay);
        animator.setRepeatCount(1);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = animFactor * (Integer) animation.getAnimatedValue();
                if (!pager.isFakeDragging()) {
                    pager.beginFakeDrag();
                }
                pager.fakeDragBy(value);
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animFactor = 1;
            }
            @Override
            public void onAnimationEnd(Animator animation) {
                pager.endFakeDrag();
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
                animFactor = -1;
            }
        });
        animator.start();
    }
}
使用示例:
animateViewPager(pager, 10, 1000);
Edit2: ValueAnimator 是 Api 级别 11 的类.在调用此方法之前还要设置寻呼机适配器.
ValueAnimator is class for Api level 11. Also set pager adapter before calling this method.
这篇关于Android Viewpager 跳到半页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android Viewpager 跳到半页
				
        
 
            
        基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				