15

我想知道ViewPagers View 是否滚动了一半屏幕?

当我拖动它时,如果它已经覆盖了一半的屏幕,我想改变页面。

例如,如果我在 MDPI 上进行测试,即屏幕宽度为 320 像素,并且如果我将 ViewPager 的页面拖动 160 像素,那么我想更改页面。

如何从 ViewPager 中获取视图覆盖的这半个距离?

4

4 回答 4

23

使用 OnPageChangeListener 的方法。如果用户滚动屏幕的一半以上,onPageScrolled 方法返回 positionOffset > 0.5。 http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

我假设您想在页面滚动一半屏幕时更新一些静态视图,而无需等待它滚动整个页面宽度。这是我使用 onPageScrolled 回调解决此问题的方法。

@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;
}
于 2013-01-01T15:33:20.047 回答
4

我同意之前的回答,但是当我在 ViewPager 上快速滑动和释放时,onPageSelected 将在 onPageScrolled 之前触发,它会调用 updateStaticViewsForScreen 3 次并查看闪烁。我通过记录 ViewPager 的状态来升级解决方案:

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels )
{
    if(mCurrentPagerState != ViewPager.SCROLL_STATE_SETTLING) 
    {

          // one of previous code of onPageScrolled         

    }
}

@Override
public void onPageSelected( int position)
{

    // one of previous code of onPageSelected   

}

@Override
public void onPageScrollStateChanged(int state) {
    mCurrentPagerState = state;
}
于 2015-06-05T09:02:51.757 回答
1

它可以在没有mNextSelectedScreen. 做你想做的事setPage()

private int mCurrentPage;

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    if (position == mCurrentPage) {
        // It's gone to right.
        if (positionOffset > 0.5) {
            setPage(position + 1);
        }
        // Not yet.
        else {
            setPage(position);
        }
    } else {
        // It's gone to left.
        if (positionOffset < 0.5) {
            setPage(position);
        }
        // Not yet.
        else {
            setPage(position + 1);
        }
    }
}

@Override
public void onPageSelected(int position) {
    mCurrentPage = position;
    setPage(position);
}
于 2015-03-17T09:09:21.343 回答
0

查看寻呼机上的 setonpagechangelistner 事件。

MyPagerAdapter adapter = new MyPagerAdapter(getApplicationContext());
            myPager = (ViewPager)findViewById(R.id.myfivepanelpager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(0);
            myPager.setOffscreenPageLimit(7);
            myPager.setOnPageChangeListener(new MyPageChangeListener());

然后

private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {

        /* (non-Javadoc)
         * @see android.support.v4.view.ViewPager.SimpleOnPageChangeListener#onPageSelected(int)
         */
        @Override
        public void onPageSelected(int position) {
            focusedPage = position;

            switch (focusedPage) {
            case 0: 
                break;
            case 1:
                                 Break;
}
}

在这里你可以得到你当前页面的位置。

于 2012-12-11T12:09:09.073 回答