简体   繁体   中英

android Disable touch when swiping in viewpager

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

    }
 }

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM