Android button random click on ViewPager widget

I have an Activity with a ViewPager with 3 dynamically created fragments. Each page has a variety of buttons and image buttons that are self-activating. Press the MotionEvent.ACTION_DOWN button.

The problem I'm running into is that these buttons get activated accidentally when I try to scroll through the ViewPager.

Can anyone recommend a way to avoid this?

Thanks Josh

+3


source to share


3 answers


Instead of using onTouch..MotionEvent.ACTION_DOWN.

in the ImageButton I'll make them implemented;



.OnItemClickListener() /This ensures that only when clicked they will call the desired functionality, I suggest you to check it out this way and let me know.

      

Hope it helps, more info; Documentation for OnItemClickListener

+2


source


I think the stream you are following is actually not very good, the button also has business code to handle the ACTION_DOWN event, and the parent (ViewPager) always needs a touch handle to detect scroll events, so the button will handle first touch the event ACTION_DOWN and then tap ViewPager.

You can move the business code to the Button's OnClickListener or some other way that you should let the button intercept the touch event and not pass it to the ViewPager (Parent View).

This can be easily dealt with by customizing the Viewpager class and adding a variable to the custom ViewPager like this:



private boolean mTouchEnable = true;

@Override
public boolean onTouchEvent(MotionEvent arg0) {
    if (!mTouchEnable) {
        return false;
    }

    return super.onTouchEvent(arg0);
}    

      

The button will have to reference the viewPager and set mTouchEnable = false to ACTION_DOWN and true to ACTION_UP

0


source


I made buttons viewpager

and I was able to replicate the problem.

I solved it like this:

My buttons have a listener OnClickListener

optionButton.setOnClickListener(this);

public void onClick(View v) {
  // your code here
}

      

Then I add this button to my customPagerAdapter

As customViewPager

you must override the method onInterceptTouchEvent

as follows:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
   super.onInterceptTouchEvent(ev);
   if (ev.getAction() == MotionEvent.ACTION_MOVE) {
      return true;
   }
   else{
      return false;
   }  
 }

      

This code will catch MotionEvent.ACTION_MOVE

in child views (returns true) and will not catch MotionEvent.ACTION_DOWN

any onClick

event in child views.

0


source







All Articles