2012-12-11 82 views
9

我想知道ViewPager的View已經滾動了一半的屏幕嗎?如何知道ViewPager滾動了一半的屏幕android

當我拖動它時,如果它覆蓋了一半的屏幕,我想更改頁面。

例如,如果我在MDPI上進行測試,即屏幕寬度爲320px,並且如果我將ViewPager的頁面拖動160px,那麼我想更改頁面。

如何從ViewPager獲得視圖覆蓋的半個距離?

回答

17

使用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; 
} 
+0

您好oLo:需要您的一些想法我必須在滑動viewpager之前檢查,如果存在然後從左到右直接進行。我浪費了很多時間來做這件事,但沒有得到完美的結果。提前致謝。 –

0

setonpagechangelistner event on view pager。

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; 
} 
} 

在這裏你可以得到你當前頁面的位置。

+0

抱歉打擾你。我不清楚我的問題。我現在更新它,請再次檢查它。謝謝 – Manoj

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); 
} 
4

我同意preious答案,但是當我快速輕掃並釋放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; 
} 
+0

這解決了我的頭痛。非常感謝。這會被認爲是一個Android的錯誤? – TigerHix

相關問題