android 在 vi​​ewpager 中滑动时禁用触摸

android Disable touch when swiping in viewpager

我在 viewpager 中有一个动画。当它在 viewpager.

中的任何地方接收到触摸事件时,它会出现并消失

问题是我想禁用滑动手势本身的动画

代码如下:

public class PageActivity extends Activity  {

    public int pagenum;
    CustomPagerAdapter mCustomPagerAdapter;
    private Animation animUp, animUp2;
    private Animation animDown, animDown2;
    RelativeLayout ll, rr;
    boolean visible = false, visible2 = false;
    ViewPager mViewPager;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_page);

       animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
       animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);
       animUp2 = AnimationUtils.loadAnimation(this, R.anim.anim_up2);
       animDown2 = AnimationUtils.loadAnimation(this, R.anim.anim_down2);

       ll = (RelativeLayout) findViewById(R.id.slider);
       ll.setVisibility(View.GONE);
       rr = (RelativeLayout) findViewById(R.id.slider2);
       rr.setVisibility(View.GONE);

       Intent extra = this.getIntent();
       pagenum = extra.getExtras().getInt("key");
       mCustomPagerAdapter = new CustomPagerAdapter(this);
       mViewPager.setAdapter(mCustomPagerAdapter);
       mViewPager.setCurrentItem(pagenum);
 }

@Override
public boolean dispatchTouchEvent( MotionEvent event){
    if(event.getActionMasked()==MotionEvent.ACTION_UP) {
        if (!visible && !visible2) {
            ll.setVisibility(View.VISIBLE);
            ll.startAnimation(animUp);
            visible = true;
            rr.setVisibility(View.VISIBLE);
            rr.startAnimation(animDown2);
            visible2 = true;
        } else {
            ll.startAnimation(animDown);
            ll.setVisibility(View.GONE);
            visible = false;
            rr.setVisibility(View.GONE);
            rr.startAnimation(animUp2);
            visible2 = false;
        }
    }
    return super.dispatchTouchEvent(event);

    }
 }

这有点老套,但您可以在 ViewPager 上放置一个 OnPageChangeListener 并监听页面拖动事件。如果你得到事件,不要做动画。

我已经更新了您的代码以获得可能的解决方案:

public class PageActivity extends Activity {

    public int pagenum;
    CustomPagerAdapter mCustomPagerAdapter;
    private Animation animUp, animUp2;
    private Animation animDown, animDown2;
    RelativeLayout ll, rr;
    boolean visible = false, visible2 = false;
    boolean dragging = false;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);

        animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
        animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);
        animUp2 = AnimationUtils.loadAnimation(this, R.anim.anim_up2);
        animDown2 = AnimationUtils.loadAnimation(this, R.anim.anim_down2);

        ll = (RelativeLayout) findViewById(R.id.slider);
        ll.setVisibility(View.GONE);
        rr = (RelativeLayout) findViewById(R.id.slider2);
        rr.setVisibility(View.GONE);

        Intent extra = this.getIntent();
        pagenum = extra.getExtras().getInt("key");
        mCustomPagerAdapter = new CustomPagerAdapter(this);
        mViewPager.setAdapter(mCustomPagerAdapter);
        mViewPager.setCurrentItem(pagenum);
        mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {

                switch (state) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    dragging = true;
                    break;
                case ViewPager.SCROLL_STATE_IDLE:
                    dragging = false;
                    break;
                }
            }

        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_UP && !dragging) {
            if (!visible && !visible2) {
                ll.setVisibility(View.VISIBLE);
                ll.startAnimation(animUp);
                visible = true;
                rr.setVisibility(View.VISIBLE);
                rr.startAnimation(animDown2);
                visible2 = true;
            } else {
                ll.startAnimation(animDown);
                ll.setVisibility(View.GONE);
                visible = false;
                rr.setVisibility(View.GONE);
                rr.startAnimation(animUp2);
                visible2 = false;
            }
        }
        return super.dispatchTouchEvent(event);

    }
}