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