禁用PagerAdapter / ViewPager中的手势

时间:2012-06-13 09:58:59

标签: android android-layout android-viewpager

首先抱歉我的英语不好。

我的工作PagerAdapter有3 Views。它工作正常。我可以使用普通手势和Buttonclicks切换视图。但我想禁用手势。是否可以仅使用buttonClicks切换视图并禁用手势?

这是PagerAdapter

public class MyPagerAdapter extends PagerAdapter {


        public int getCount() {
            return 3;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.back;
                break;
            case 1:
                resId = R.layout.stock;
                break;
            case 2:
                resId = R.layout.menu;
                break;
            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2); 

        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);

        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }

已解决

    final View touchView = findViewById(R.id.remotePager);
    touchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return true;
        }
    });

1 个答案:

答案 0 :(得分:9)

覆盖onTouchListener()并返回false以禁用滑动分页。

public boolean onTouchEvent(android.view.MotionEvent ev) {
        if (super.onTouchEvent(ev)) {
            return false;
        }
}