24

I'm trying to get familiar with ViewPager. My code is given below. The problem is that the onPageChangeListener methods are not being called. What can be the problem?

public class TabsViewPagerFragmentActivity extends SherlockFragmentActivity implements ViewPager.OnPageChangeListener {


private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private PageIndicator mIndicator;

private static final String[] CONTENT = new String[] { "Home", "Popular", "Chat", "Profile"};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();

    View customview = LayoutInflater.from(this).inflate(R.layout.actionbar_custom_layout, null);

    ImageView im = (ImageView)customview.findViewById(R.id.add);

    /*im.getLayoutParams().height = h;
    im.getLayoutParams().width = h;*/

    bar.setCustomView(customview);
    bar.setDisplayShowCustomEnabled(true);
    bar.setDisplayShowTitleEnabled(false);
    bar.setDisplayShowHomeEnabled(false);
    // Inflate the layout
    setContentView(R.layout.tabs_viewpager_layout);


    // Intialise ViewPager
    this.intialiseViewPager();

    mIndicator = (TitlePageIndicator)findViewById(R.id.titles);
    mIndicator.setViewPager(mViewPager);

    TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.titles);

    indicator.setTextColor(0xAAfb891e);
    indicator.setSelectedColor(0xFFfb891e);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    menu.add("Search")
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


    return true;
}



public int getCount() {
    return CONTENT.length;
}


protected void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
}
private void intialiseViewPager() {


    this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(),
                                            fragments);

    this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(this.mPagerAdapter);
    this.mViewPager.setOnPageChangeListener(this);



}

public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
    // TODO Auto-generated method stub

    System.out.println(positionOffsetPixels);

}

public void onPageSelected(int position) {
    // TODO Auto-generated method stub

    System.out.println(position);
}


public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

    System.out.println(state);

}
}

What I mean is that I dont get anything printed in the Logcat on scrolling my pages.

ProgrAmmar
  • 3,045
  • 4
  • 19
  • 26

4 Answers4

119

Well I solved the problem myself after some trial error.

Since I was introducing a TitlePagerIndicator 'mIndicator', I had to call mIndicator.setOnPageChangeListener(this); and not mViewPager.setOnPageChangeListener(this);

ProgrAmmar
  • 3,045
  • 4
  • 19
  • 26
  • 5
    Thanks. It saves my head. But, could you please tell me the reason? – Nguyen Minh Binh Dec 02 '12 at 10:46
  • 9
    I imagine the OP is using something like http://viewpagerindicator.com/ that internally sets the `onPageChangeListener` and so any that is set programmatically will be dereferenced – Dori Feb 22 '13 at 15:00
  • YES, FINALLY. I'm using viewpagerindicator lib and the listener has to be attached to the indicator and not to the viewpager. Thanks! – Roberto May 16 '14 at 01:23
  • thanks it helped, but I was wondering why it wasn't working with ViewPager – Syed Raza Mehdi Jun 17 '14 at 14:13
  • The ViewPager is only capable of handling a single OnPageChangeListener. Whenever you call ViewPager.setOnPageChangeListener(...) the previously set one gets removed. So it's either the PageIndicator's listener or your custom one. If you set it on PageIndicator, the PageIndicator will simply forward all incoming listener calls to your custom listener, therefore avoiding the single-listener limitation. – MrMaffen Dec 13 '14 at 19:38
2

The System.out stream doesn't print to Logcat. Use the Log class instead like Log.d("MyApp","message").

Chris Fei
  • 1,327
  • 8
  • 11
  • I have used System.out.println() previously to print dynamic values in the logcat before. But then too ill try this. – ProgrAmmar Jun 16 '12 at 16:26
0

The way you have written your code i guess you are implementing ViewPager.OnPageChangeListener

Change

this.mViewPager.setOnPageChangeListener(mIndicator);

To

this.mViewPager.setOnPageChangeListener(this);

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • Actually it was "this" only, I had just changed it to see the effects. But anyways, changing mIndicator to this didnt help. – ProgrAmmar Jun 16 '12 at 16:20
0

I followed ProgrAmmar's tip, but it didn't work for me. In my case, I used

mIndicator.setCurrentItem(position);

and it worked fine.

DarkMukke
  • 2,469
  • 1
  • 23
  • 31
Leonardo Costa
  • 994
  • 11
  • 26