我有viewPager
使用FragmentStatePagerAdapter
作为适配器,在片段类中使用getItem()
适配器函数我在运行时使用片段管理器添加一些其他片段。正确显示了页面零(起始页)的碎片,但是对于任何其他页面我应该滑过它然后再刷回它以查看内容。(因为我检查每次滑动导致内部碎片和碎片管理器表现正确,即使是第一次但是最初没有显示任何内容)
任何解决方案都很受欢
提前致谢
主类中的适配器代码
class FragmntProducer extends FragmentStatePagerAdapter{
public FragmntProducer(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return new PagerFragment(pager.getCurrentItem());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
}
用于viewpager的片段类
public class PagerFragment extends Fragment {
WeekView rowOne , rowTwo , rowThree , rowFour , rowFive , rowSix;
int cis;
public PagerFragment(int ci) {
// TODO Auto-generated constructor stub
cis = ci;
// Toast.makeText(getContext(), ci+"under construct", Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.pager_fragment_layout, container, false);
Toast.makeText(getContext(),"page: "+cis,Toast.LENGTH_SHORT).show();
// rowOne = new WeekView();
// getFragmentManager().beginTransaction().add(R.id.week_one, rowOne).commit();
rowTwo = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_two, rowTwo).commit();
// rowThree = new WeekView();
// getFragmentManager().beginTransaction().add(R.id.week_three, rowThree).commit();
rowFour = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_four, rowFour).commit();
rowFive = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_five, rowFive).commit();
rowSix = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_six, rowSix).commit();
super.onCreateView(inflater, container, savedInstanceState);
return layout;
}
}
viewPager片段的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="@+id/week_one"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="@+id/week_two"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<fragment
android:id="@+id/week_three"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="@+id/week_four"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<FrameLayout
android:id="@+id/week_five"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="@+id/week_six"
android:layout_width="match_parent"
android:layout_height="40dp"/>