当用户向后滑动ViewPager的第一页时,显示Toast“这是第一页”

时间:2016-09-14 12:31:42

标签: android android-viewpager

最后一页我的代码正在运行,但我也想要第一页。如果用户向后滑动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;


            }
        });

1 个答案:

答案 0 :(得分:0)

我在我的viewpager类中添加了这个问题的答案

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

在viewpager的 onIntercepttouchevent

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