android Disable touch when swiping in viewpager

750 views Asked by At

I have an animation inside a viewpager. It appears and disappears when it receives a touch event anywhere in the viewpager.

The problems is that I would like to disable the animation for the swiping gesture itself

Here is the code:

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

    }
 }
1

There are 1 answers

0
kris larson On BEST ANSWER

This is kinda hackish, but you could put an OnPageChangeListener on the ViewPager and listen for the page dragging event. If you get the event, don't do the animation.

I've updated your code for a possible solution:

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

    }
}