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.
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.
How to get this half distance covered by view from ViewPager?
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
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;
}
I agree with preious answer, but when I quick swipe and release on ViewPager, onPageSelected will be trigged before onPageScrolled and it will call updateStaticViewsForScreen 3 times and view blinks. I upgrade solution with recording ViewPager's state:
@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;
}
It can be achieved without mNextSelectedScreen
. Do what you want on 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With