android在viewpager中滑动时禁用触摸

时间:2015-06-24 22:16:21

标签: android android-viewpager viewpagerindicator

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

    }
 }

1 个答案:

答案 0 :(得分:0)

这有点像hackish,但你可以在OnPageChangeListener上加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;
    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);

    }
}