2016-09-14 40 views
1

對於最後一頁,我的代碼正在工作,但我也想爲第一頁。如果用戶向後滑動瀏覽第一頁viewpager,如何向用戶顯示Toast 「這是第一頁」。這是我的頁面更改偵聽代碼:當用戶向後滑動ViewPager的第一頁時,顯示Toast「這是第一頁」

 verticalViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      boolean lastPageChange = false; 
      boolean firstPageChange = false; 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

       int lastIdx = verticalViewPagerAdapter.getCount() - 1; 
       int firstIdx = 0; 

       Log.d("setPageTransformer", "pos:" + position); 
       if(lastPageChange && position == lastIdx) { 
        Toast.makeText(VerticalViewPagerActivity.this, "Thanks, End of feed reached!", Toast.LENGTH_SHORT).show(); 
       } 
       else if (firstPageChange && position == firstIdx){ 
        Toast.makeText(VerticalViewPagerActivity.this, "No new articles!", Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      public void onPageSelected(int position) { 

      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 
       int lastIdx = verticalViewPagerAdapter.getCount() - 1; 
       int curItem = verticalViewPager.getCurrentItem(); 
       int firstIdx = 0; 

//    int curItem = pgScreen.getCurrentItem(); 
       if(curItem==lastIdx && state==1) 
        lastPageChange = true; 
       else 
        lastPageChange = false; 

       if (curItem==firstIdx && state==1) 
        firstPageChange = true; 
       else 
        firstPageChange = false; 


      } 
     }); 

回答

0

我得到的回答這個問題,我在我的viewpager類從而增加

public void setOnSwipeOutListener(OnSwipeOutListener listener) { 
     mListener = listener; 
    } 
public interface OnSwipeOutListener { 
     public void onSwipeOutAtStart(); 
     public void onSwipeOutAtEnd(int position); 
    } 

而且在onIntercepttouchevent viewpager的

boolean callHappened = false; 
    case MotionEvent.ACTION_MOVE: { 

       if (!callHappened) { 
        if (mStartDragX < xn && getCurrentItem() == 0) { 
         mListener.onSwipeOutAtStart(); 
         callHappened = true; 
        } else if (mStartDragX > xn && getCurrentItem() == getAdapter().getCount() - 1) { 
         mListener.onSwipeOutAtEnd(getCurrentItem()); 
         callHappened = true; 
        } 
       } 

和現在我可以在我的活動這樣使用。

verticalViewPager.setOnSwipeOutListener(new VerticalViewPager.OnSwipeOutListener() { 
      @Override 
      public void onSwipeOutAtStart() { 
      Snackbar snackbar = Snackbar.make(parentLayout, "This is  first position, Snackbar.LENGTH_SHORT); 
       snackbar.show(); 
      } 
     @Override 
      public void onSwipeOutAtEnd(int position) { 
       Snackbar snackbar = Snackbar.make(parentLayout, getString(R.string.top_shot_message_bookmark), Snackbar.LENGTH_SHORT); 
       snackbar.show(); 
      } 
}); 
相關問題