1

I am going mad and yet not understanding what mistake am I making, Please help.

I have a ViewPager in my layout xml, I have a FragmentStatePagerAdapter for this pager. Basically am loading different fragments on this ViewPager.

But the number of fragments will be decided by users certain action and to start off with ViewPager will not load any fragments, I mean FragmentStatePagerAdapter count will be 0.

Now I want user to long hold ViewPager to trigger some action. But when I add OnLongClickListener to viewpager onLongClick method never triggers :(

Here is my code,

pageView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
            Log.d("sandeep","YES");
            return false;
      }
});

What is the mistake here?? Please help. Thanks in advance.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • return true insted of false – ak sacha Apr 01 '16 at 12:00
  • @ak sacha: Control never reaches here :( no point in returning true or false here – Sandeep Bhandari Apr 01 '16 at 12:02
  • 1
    ok, so then you can set the LongClickListener on the layout that you're inflating. – ak sacha Apr 01 '16 at 12:12
  • @ak sacha: I really appreciate your effort you are putting in to help me :) but a small problem here, Issue is the layout contains just a PageViewer, So ViewPagerCovering whole view of my layout all touches are being inercepted by ViewPager there is no way my layout gets any touch :( I hope I made my point clear :) If you din get please lemme know I'll try explain better :) – Sandeep Bhandari Apr 01 '16 at 12:13

2 Answers2

1

OnClickListener and OnLongClickListener doesn't work with the ViewPager itself because it's capturing touch events to be able to scroll between pages.

You should set an onTouchListener, capture the ACTION_UP motion event. And don't forget to remove the listener when the pager has items.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • I realized that OnTouchListener gets triggered properly on ViewPager but requirement is I want user to hold on to ViewPager for sometime to trigger some actions (like on holding for 2 seconds show some menu) How can I achieve it with OnTouchListener ?? Anyway up voting your answer good observation :) – Sandeep Bhandari Apr 01 '16 at 12:02
  • 1
    This is not a very good solution, but this might work. Take a button having width and height = `fill_parent` and then make the button background transparent. Put the `onLongClickListener` with that button. – Reaz Murshed Apr 01 '16 at 12:15
  • 1
    Check this answer, I think is what you are looking for: http://stackoverflow.com/questions/7919865/detecting-a-long-press-with-android/11679788#11679788 @ReazMurshed answer is also another good option. – emanzanomarcos Apr 01 '16 at 12:15
  • @Reaz Murshed,emanzanomarcos : hehehehehe :) I just did the same trick with a view rather than the button :) true that it works but somehow am not feeling that its correct :( emanzanomarcos : ok lemme check your link – Sandeep Bhandari Apr 01 '16 at 12:20
0

On viewPager you can set OnTouchListener to get the gestureDetector working for longpress detection. But I would suggest you use this on the fragment parent layout that is being loaded into the viewpager.

public class MainActivity extends Activity {

    private static final String DEBUG_TAG = "Gestures";
    private GestureDetectorCompat mDetector; 

    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Instantiate the gesture detector with the
        // application context and an implementation of
        // GestureDetector.OnGestureListener
        mDetector = new GestureDetectorCompat(this,new Gesture());

        yourViewPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                 return mDetector.onTouchEvent(event);
            }
        });
    }

  class Gesture extends GestureDetector.SimpleOnGestureListener{
       public boolean onSingleTapUp(MotionEvent ev) {
       }

       public void onLongPress(MotionEvent ev) {
         //your long press code here
       }

       public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
       float distanceY) {
       } 

       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
       float velocityY) {
       }
   }

}
Nanites
  • 410
  • 3
  • 16