Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTouchEvent is getting executed when page is swiped using ViewPager and PassByValue

In my project have used onTouchEvent for detecting touch on the screen and doing action accordingly , and have used viewPager for swipe action. Problem is on swipe the touch actions are getting executed. so find the solution from here solved it. But my new Problem to it is the touch is getting disabled after once TouchEvent.Action_up gets executed. code is as follows:

parent.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {

        switch(event.getAction())
        {
         case MotionEvent.ACTION_MOVE: 
                awesomePager.requestDisallowInterceptTouchEvent(true);
                break;
        case MotionEvent.ACTION_UP:
            awesomePager.requestDisallowInterceptTouchEvent(true);
             if(flag)
             {
               upperdock.setClickable(false);
                upperdock.bringToFront();
                tocparent.bringToFront();
                tocbottom.bringToFront();
                upperdock.setVisibility(RelativeLayout.VISIBLE);
                tocparent.setVisibility(LinearLayout.VISIBLE);
                tocbottom.setVisibility(LinearLayout.VISIBLE);
                flag=false;
             }
             else
             {

                parent.bringToFront();
                upperdock.setVisibility(RelativeLayout.INVISIBLE);
                tocparent.setVisibility(LinearLayout.INVISIBLE);
                tocbottom.setVisibility(LinearLayout.INVISIBLE);
                flag=true;
             }
             break;

        case MotionEvent.ACTION_CANCEL:
            awesomePager.requestDisallowInterceptTouchEvent(false);
             break;
        default:
            break;
        }

        return false;
    }
});

In the above code if i return false Action_up is not getting executed.. if i return true Action_cancel is not getting executed..that is pass by value is the problem in there.

like image 275
Zombie Avatar asked May 31 '12 06:05

Zombie


People also ask

How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

How do I stop ViewPager from sliding on Android?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.

Can I use ViewPager with views not with fragments?

yes...you can use View instead of Fragment in viewpager. Here you can Find Whole example that will help you to achieve Viewpager without Fragment. Go through this documentation. Save this answer.

How do I swipe ViewPager on Android?

Implement Swipe Views You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


2 Answers

i change something for this.

not use awesomePager.requestDisallowInterceptTouchEvent(false), just use return true.

in the activity define a boolean sroll ;

set sroll = positionOffset != 0.0 in ViewPager.onPageScrolled and i override onTouchEvent in which the view in the ViewPager.

now you can check if (event.getAction()==MotionEvent.ACTION_UP && !sroll) to decide the event whether will triggered

like image 140
huntersea Avatar answered Nov 15 '22 00:11

huntersea


I had the same problem! For me this worked very well....

case MotionEvent.ACTION_MOVE: {
        getParent().requestDisallowInterceptTouchEvent(false);
        break;
    }

 case MotionEvent.ACTION_CANCEL:{
        getParent().requestDisallowInterceptTouchEvent(true);
        break;

}

case MotionEvent.ACTION_UP:{

 //delete  awesomePager.requestDisallowInterceptTouchEvent(true);
 //you don't need it here!
                 .
                 .
            do your stuff....
                 .

}

return your whole onTouch methode as true and not false...

return true;
like image 44
gewhjhfkgad Avatar answered Nov 14 '22 23:11

gewhjhfkgad