ViewPager不会滚动片段:
我在活动中包含了ViewPager。 并创建两个片段,但当我滚动视图寻呼机(horizintal)时,只有第一个Framgent选择:
_pageAdapter=new PagerAdapter(getSupportFragmentManager());
_viewPager= (ViewPager) findViewById(R.id.viewPager);
_viewPager.setAdapter(_pageAdapter);
_viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d(LOG_TAG,"!Page #"+position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
if(state==0)
Log.d(LOG_TAG,"Page Iddle");
else if(state==1)
Log.d(LOG_TAG,"Page Dragging");
else if(state==2)
Log.d(LOG_TAG,"Page Settling");
}
});
这里是PageAdapter的代码:
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0: {
fragment = new FirstFramgent();
Bundle bundle = new Bundle();
bundle.putInt("num", 0);
fragment.setArguments(bundle);
break;
}
case 1: {
fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putInt("num", 1);
fragment.setArguments(bundle);
break;
}
}
return fragment;
}
@Override
public int getCount() {
return 1;
}
}
告诉我,如何让ViewPager滚动两个片段。
答案 0 :(得分:2)
尝试此更改:
@Override
public int getCount() {
return 2;
}