android ViewPager与Zoom ImageView

时间:2017-04-24 10:57:17

标签: java android

我用缩放来编写ViewPager来缩放ImageView,简单地说,需要一种方法来在图像放大时禁用分页并在显示原始缩放图像时启用它。但是当我处理onTouchEvent时返回false然后viewPager.setOnPageChangeListener不起作用怎么解决?以下是我的代码,谢谢

   public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context) {
        super(context);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    }
viewPager.setOnPageChangeListener(new OnPageChangeListener(){
        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if(getScale() > 0.69){
                viewPager.setPagingEnabled(false);
            }else{
                viewPager.setPagingEnabled(true);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
    });

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助。我没有测试过。所以不能真的说它是否会起作用。 如果有效,请告诉我。

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context) {
    super(context);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }
    return super.onTouchEvent(null);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }
    return super.onInterceptTouchEvent(null);
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}}