OnTouchEvent fires when a page scrolls with ViewPager and PassByValue

In my project, I used onTouchEvent to detect a touch on the screen and perform actions accordingly, and used the viewPager for the swipe action. The problem is that sensory actions are performed. so I decided to find a solution from here . But my new problem is that the touch becomes disabled after TouchEvent.Action_up is executed. The 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 executed. If I return true, Action_cancel is not executed. that is, the value of pass is the problem there.

+7
source share
2 answers

I changed something for this.

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

in activity determine the Boolean scroll ;

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

now you can check if (event.getAction () == MotionEvent.ACTION_UP &! scroll) to decide if it will be called

+2
source

I had the same problem! It worked out very well for me ....

 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 the whole onTouch method as true, not false ...

 return true; 
+1
source

All Articles