Nancy
Nancy

Reputation: 153

How to enable tap listener in view pager in Android?

I am using view pager to swipe between the views in Android. Now I need to capture tap event for each of the views. when I override the touch listener to capture the tap event, the swipe action doesn't happen and the screen remains in the first page itself. How do I add touch listener to view pager?

Code:

viewPager.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

                     mDetector.onTouchEvent(event);
                     return true;   



            }});

For the above code I am able to capture tap event, but the swipe action becomes Impossible.

Upvotes: 8

Views: 14547

Answers (5)

Narendra Singh
Narendra Singh

Reputation: 4001

Put the click event on the item view of the viewpager inside the viewPagerAdapter in the method instantiateItem like -

       @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;
        }

Upvotes: 0

stoiczek
stoiczek

Reputation: 1363

Just to add to Jorge's great answer, you may just use distance instead of sameX and sameY, which is a bit more elegant. Sample:

// 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;
}

Upvotes: 0

Jorge Aguilar
Jorge Aguilar

Reputation: 3450

Here i leave you a snippet from my code to detect a "click" on the OnTouchListener, i 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;
        }
    });

Upvotes: 27

LOG_TAG
LOG_TAG

Reputation: 20569

We can use Gestures (Link1, Link2):

public boolean onTouchEvent (MotionEvent ev)

Hope this helps!

Upvotes: 4

Soham
Soham

Reputation: 4960

Nancy, you don't need to manually override the Page swipes or the touch events. Just add the pages to the ViewPager and the ViewPager will automatically take care of swiping.

You do, however, have to attach touch listeners to the object in each page. So if Page 1 has a Linear Layout with many buttons and you need to find out when those buttons are clicked, you need to attach OnClickListeners for each of those buttons.

Do let me know your use case so we can better understand, why you need to find out when a page has been clicked!

Upvotes: 2

Related Questions