How to enable the player to listen to peer in Android?

I use pager view to scroll between views in Android. Now I need to fix the transition event for each of the views. when I redefine the touch listener to capture the click event, the swipe action does not occur and the screen remains on the first page. How to add a touch listener to view a pager?

the code:

viewPager.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { mDetector.onTouchEvent(event); return true; }}); 

For the code above, I can capture the tap event, but the swipe action becomes impossible.

+6
source share
5 answers

Here I leave you a code snippet to detect the "click" in OnTouchListener, hope it helps

 mImagePager.setOnTouchListener(new OnTouchListener() { private float pointX; private float pointY; private int tolerance = 50; @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_MOVE: return false; //This is important, if you return TRUE the action of swipe will not take place. case MotionEvent.ACTION_DOWN: pointX = event.getX(); pointY = event.getY(); break; case MotionEvent.ACTION_UP: boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX(); boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY(); if(sameX && sameY){ //The user "clicked" certain point in the screen or just returned to the same position an raised the finger } } return false; } }); 
+23
source

We can use Gestures ( Link1 , Link2 ):

 public boolean onTouchEvent (MotionEvent ev) 

Hope this helps!

+4
source

Nancy, you donโ€™t need to manually override page scrolling or touch events. Just add pages to the ViewPager, and ViewPager will automatically take care of scrolling.

However, you need to connect touch listeners to the object on each page. So, if on page 1 there is a linear layout with many buttons, and you need to know when these buttons will be pressed, you need to attach OnClickListeners to each of these buttons.

Let me know your use case so that we can better understand why you need to know when the page was clicked!

+2
source

Just to add a great answer to Jorge, you can just use the distance instead of the same X and sameY, which is a bit more elegant. Example:

 // Ignore events that are swipes rather then touches float distX = event.getX() - pointX; float distY = event.getY() - pointX; double dist = Math.sqrt(distX * distX + distY * distY); if (dist > tolerance) { return false; } 
0
source

Place the click event on the view of the viewpager element inside the viewPagerAdapter in the instantiateItem like method -

  @Override public Object instantiateItem(ViewGroup container, final int position) { // Declare Variables ImageView jive_image; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.list_item_viewpager, container, false); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); // Add viewpager_item.xml to ViewPager ((ViewPager) container).addView(itemView); return itemView; } 
0
source

Source: https://habr.com/ru/post/909974/


All Articles