Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager OnLongClick listener not firing

Tags:

android

I have a fragment which contains a ViewPager. When I inflate the layout, I assign an OnLongClick listener to it as follows:

mPager.setOnLongClickListener(mOnPagerLongClickListener);

However, when I perform a long click on the ViewPager, nothing happens. What can I do to make this work? Or do I need to assign the listener to every view in the ViewPager instead? Assigning the listener to the GridViews which the ViewPager contains doesn't seem to work either.

like image 759
Catherine Avatar asked Feb 03 '12 00:02

Catherine


1 Answers

Excuse for putting on a query, but I am able to solve the issue. It won't be helpful to you but may come in handy for any other viewer.

The simple solution is to assign listener directly to ImageView's object rather assigning it to ViewPager's object, i.e, assigning viewPager.setOnLongClickListener will not fire anything.

So, we have to initialize ImageView with onLongClickListeners in the class extending PageAdapter in instantiateItem() by:

imageView.setOnLongClickListener(new OnLongClickListener()){

    @Override
    public boolean onLongClick(View v) {
        // Do your stuff
        return false;
    }
});
like image 60
abhy Avatar answered Oct 18 '22 10:10

abhy