Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPageSelected is never called for first item?

The problem I faced is that the ViewPager is not calling onPageSelected for the 0 item. Code sample :

public class PagerActivity extends ActionBarActivity implements ViewPager.OnPageChangeListener {

private static final String TAG = PagerActivity.class.getCanonicalName();

private ViewPager mViewPager;

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

    setContentView(R.layout.activity_pager);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(new ViewAdapter());
    mViewPager.setOnPageChangeListener(this);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    Log.e(TAG, "position = " + position);

}

@Override
public void onPageSelected(int position) {
    Log.e(TAG, "selected page = " + position);

}

@Override
public void onPageScrollStateChanged(int state) {
    Log.e(TAG, "state = " + state);

}

private class ViewAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(container.getContext())
                .inflate(R.layout.view_pager_item, container, false);
        TextView textView = (TextView) viewGroup.findViewById(R.id.page_indicator);
        textView.setText("Page position = " + position);
        container.addView(viewGroup);
        return viewGroup;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ViewGroup) object);
    }
}

I have no specific task related to that, just wanted to know :

  • Is it a known issue and what is the best solution?

  • Is it just designed to work this way?

like image 710
Yurii Tsap Avatar asked Jan 08 '16 12:01

Yurii Tsap


1 Answers

Add this under OnPageChangeListener

pageChangeListener.onPageSelected(0); //Set start to 0 page

In your case:

PagerActivity.onPageSelected(0);

However you should remove the implements ViewPager.OnPageChangeListener from the class and add this:

OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
    @Override
    public void onPageScrollStateChanged(int arg0) { }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    @Override
    public void onPageSelected(int position) {

        switch (position) {
        case 0: 
                Log.e(TAG, "selected page = " + position);
            break;

        default:

                Log.e(TAG, "selected page = " + position);
            break;
        }
    }
});
mViewPager.setOnPageChangeListener(pageChangeListener);
pageChangeListener.onPageSelected(0);
like image 132
Michele Lacorte Avatar answered Nov 18 '22 17:11

Michele Lacorte