我遇到的问题是ViewPager没有调用0项的onPageSelected方法。
代码示例:
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);
}
}
我没有与此相关的具体任务,只是想知道:
这是否是已知的问题,有什么最佳解决方案?
这是否只是按照设计方式工作的?