How to know ViewPager has scrolled half the screen in android(如何知道 ViewPager 在 android 中滚动了一半的屏幕)
问题描述
我想知道ViewPager
的View是否滚动了半屏?
I want to know that the ViewPager
s View has scrolled half the screen or not?
当我拖动它时,如果它已经覆盖了一半的屏幕,我想改变页面.
While I am dragging it, if it has covered half the screen I want to change the page.
例如如果我在 MDPI 上进行测试,即屏幕宽度为 320 像素,并且如果我将 ViewPager 的页面拖动 160 像素,那么我想更改页面.
For e.g. if I am testing on MDPI, i.e. of screen width 320px, and if I drag my page of ViewPager by 160px then I want to change the page.
如何从 ViewPager 获取视图覆盖的这半个距离?
How to get this half distance covered by view from ViewPager?
推荐答案
使用 OnPageChangeListener 的方法.如果用户滚动屏幕的一半以上,onPageScrolled 方法返回 positionOffset > 0.5.http://developer.android.com/reference/android/support/v4/视图/ViewPager.OnPageChangeListener.html
Use OnPageChangeListener's methods. The onPageScrolled method returns positionOffset > 0.5 if user scrolled more than half of the screen. http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
我假设您想在页面滚动一半屏幕时更新一些静态视图,而无需等待它滚动整个页面宽度.这是我使用 onPageScrolled 回调解决此问题的方法.
I assume You want to update some static views when page is scrolled by half of screen without waiting for it to scroll full page width. Here is my solution for this problem using onPageScrolled callback.
@Override
public void onPageScrolled( int position , float positionOffset , int positionOffsetPixels )
{
if ( position == mCurrentSelectedScreen )
{
// We are moving to next screen on right side
if ( positionOffset > 0.5 )
{
// Closer to next screen than to current
if ( position + 1 != mNextSelectedScreen )
{
mNextSelectedScreen = position + 1;
updateStaticViewsForScreen( mNextSelectedScreen );
}
}
else
{
// Closer to current screen than to next
if ( position != mNextSelectedScreen )
{
mNextSelectedScreen = position;
updateStaticViewsForScreen( mNextSelectedScreen );
}
}
}
else
{
// We are moving to next screen left side
if ( positionOffset > 0.5 )
{
// Closer to current screen than to next
if ( position + 1 != mNextSelectedScreen )
{
mNextSelectedScreen = position + 1;
updateStaticViewsForScreen( mNextSelectedScreen );
}
}
else
{
// Closer to next screen than to current
if ( position != mNextSelectedScreen )
{
mNextSelectedScreen = position;
updateStaticViewsForScreen( mNextSelectedScreen );
}
}
}
}
@Override
public void onPageSelected( int arg0 )
{
updateStaticViewsForScreen( arg0 );
mCurrentSelectedScreen = arg0;
mNextSelectedScreen = arg0;
}
这篇关于如何知道 ViewPager 在 android 中滚动了一半的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何知道 ViewPager 在 android 中滚动了一半的屏幕
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01