viewpager屏幕向右或向左滑动?

时间:2014-07-17 09:49:44

标签: android

我如何知道用户是否在视图寻呼机中向右或向左滑动屏幕?

indicator.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected (int page)
    {
        //page changed
    }

    @Override
    public void onPageScrolled (int arg0, float arg1, int arg2)
    {
    }

    @Override
    public void onPageScrollStateChanged (int arg0)
    {
    }
});

我使用这种方法,我很确定onpagescrollstatechanged是告诉我们它的右边或左边但是我们如何使用它的那个?

4 个答案:

答案 0 :(得分:4)

试试:

@Override
public void onPageSelected(int arg0) {
    if(lastPage>arg0)
      {//User Move to left}
    else(lastPage<arg0) 
     {//User Move to right}
      lastPage=arg0
}

答案 1 :(得分:2)

将值单独保存在另一个变量中,并将其与onpagescrollstate进行比较。

OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
                    if(savePosition>arg0)
                    {
                        Toast.makeText(getApplicationContext(), "Swiped Right", Toast.LENGTH_SHORT).show();
                    }else
                    {
                        Toast.makeText(getApplicationContext(), "Swiped Left", Toast.LENGTH_SHORT).show();
                    }
                    savePosition = arg0;
                }
            };

答案 2 :(得分:1)

  1. 这是我在 ViewPager.OnPageChangeListener onPageScrolled() 方法中的解决方案: enter image description here

答案 3 :(得分:0)

如果您想知道用户是实时向左还是向右滚动。你可以这样做:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        float tempPositionOffset = 0;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == 0) {
                if (tempPositionOffset < positionOffset) {
                    Log.d("eric", "scrolling left ...");
                } else {
                    Log.d("eric", "scrolling right ...");
                }

                tempPositionOffset = positionOffset;

          Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";");
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });