如何知道 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 中滚动了一半的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)