how to disable viewpager adapter on touching specific views?

前端 未结 4 1373
长发绾君心
长发绾君心 2020-12-01 02:59

I have a viewpager which switches between tabs when swiping left/right.In my second tab, i have some custom views which have listeners for pinching and dragging but when i t

相关标签:
4条回答
  • 2020-12-01 03:37

    If you know the positions on which you don't want to intercept touch events in the view pager you can do something like I did.

     @Override
     public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if(arg0.getY()>getHeight()/2)return false;
        return super.onInterceptTouchEvent(arg0);
    }
    
    0 讨论(0)
  • 2020-12-01 03:38

    This first thing that comes to mind for me is to have a custom ViewPager in which, when your touch listeners get notified of a specific event you could set the swipeable boolean in ViewPager to false and set it back to true whichever way best fits your application.

    public class CustomViewPager extends ViewPager {
        private boolean swipeable = true;
    
        public CustomViewPager(Context context) {
            super(context);
        }
    
        public CustomViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        // Call this method in your motion events when you want to disable or enable
        // It should work as desired.
        public void setSwipeable(boolean swipeable) {
            this.swipeable = swipeable;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent arg0) {
            return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false; 
        }
    
    }
    

    Make sure to change your layout file to show:

    <com.your.package.CustomViewPager .. />
    

    Instead of:

    <android.support.v4.view.ViewPager .. />
    

    Edit 2

    Here is my setup (Working with the above CustomViewPager):

    CustomViewPager mViewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
        // Set up the CustomViewPager with the sections adapter.
        mViewPager = (CustomViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
    
        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    
    }
    
    public void swipeOn(View v) {
        mViewPager.setSwipeable(true);
    }
    
    public void swipeOff(View v) {
        mViewPager.setSwipeable(false);
    }
    

    The above shown onCreate is in my MainActivity class which extends FragmentActivity and implements ActionBar.TabListener

    0 讨论(0)
  • 2020-12-01 03:42

    I am using requestDisallowInterceptTouchEvent(true) int the onTouchEvent listener of the view that also has drag events.

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        ViewParent parent = getParent(); 
        // or get a reference to the ViewPager and cast it to ViewParent
    
        parent.requestDisallowInterceptTouchEvent(true);
    
        // let this view deal with the event or
        return super.onTouchEvent(event);
    }
    
    0 讨论(0)
  • 2020-12-01 03:46

    I am using like this

    public void setSwipeable(boolean swipeable)
    {
        this.swipeable = swipeable;
    }
    
    @Override
    public void scrollTo(int x, int y){
        if (swipeable){
            super.scrollTo(x, y);
        }
    }
    
    0 讨论(0)
提交回复
热议问题