2

How can I set view pager's on Item Click Listener. I want to detect click on view pager's current item. Set On Click Listener is not working.

I am having Direct View Pager in my application and i am setting its adapter with a class that extends fragment and assigning that fragment to view pager's adapter. Now I want to detect item click or I can say individual fragment click so how can I do that?

please anyone help me I am badly stuck into this problem and not finding any related solution. I have done so much search but not able to find solution for this.

Shahrzad
  • 1,062
  • 8
  • 26
Ravi
  • 93
  • 3
  • 11
  • Did you try: `vPager.setOnLongClickListener(new OnLongClickListener()){ @Override public boolean onClick(View v) { // Do something return false; } });` - or what have you tried ? – g00dy Jul 30 '13 at 06:54
  • @g00dy i have tried on Click Listener which is not working for individual fragment item click.and view pager is not allowing to set on touch listener – Ravi Jul 30 '13 at 07:01
  • do you have an error output or how come it's not allowing that? – g00dy Jul 30 '13 at 07:04
  • There's also a logged bug for this OcClick listener in a specific situation, see if that's you case: https://code.google.com/p/android/issues/detail?id=37256&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars – g00dy Jul 30 '13 at 07:05
  • @g00dy when i am clicking on any item nothing happens.code inside the on click listener is not getting executed. if you say i can upload the entire code. – Ravi Jul 30 '13 at 07:06

2 Answers2

2

You can set the onClickListener on the individual pages of the view Pager within the instantiateItem method I have tried for simple images within a view pager

 @Override
public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View page = inflater.inflate(R.layout.YOUR_PAGE, null);

    page.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + pos);
        }
    });


    ((ViewPager) collection).addView(page, 0);
    return page;
}

you can refer to the original post here ViewPager OnItemClickListener

Salil Kaul
  • 261
  • 2
  • 7
-1

You can do this by implement OnPageChangeListener

public class LeftPanelPagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener{
        .....
        .....
        ....

private int currentSelectedFragmentPosition = 0; 
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    public void onPageSelected(int arg0) {
        currentSelectedFragmentPosition = arg0;

    }

    public int getCurrentSelectedFragmentPosition() {
        return currentSelectedFragmentPosition;
    }



}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165