如何知道 ViewPager 在 android 中滚动了一半的屏幕

How to know ViewPager has scrolled half the screen in android(如何知道 ViewPager 在 android 中滚动了一半的屏幕)

本文介绍了如何知道 ViewPager 在 android 中滚动了一半的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道ViewPager的View是否滚动了半屏?

I want to know that the ViewPagers 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 中滚动了一半的屏幕

基础教程推荐