如何在fragment里面嵌套viewpager
1个回答
展开全部
fragment1 Class One对应的布局文件 one.xml
<android.support.v4.view.ViewPager
android:id="@+id/one_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
fragment1对应的类Class One:
public class One extends Fragment {
private List<Fragment> list = new ArrayList<Fragment>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.one, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager vp = (ViewPager) getView().findViewById(R.id.one_vp);
list.add(new FragOne());//子fragment
list.add(new FragTwo());//子fragment
VpAdapter adapter = new VpAdapter(getChildFragmentManager(), list);
vp.setAdapter(adapter);
}
}
漏掉VpAdapter类了:
public class VpAdapter extends FragmentPagerAdapter {
private List<Fragment> list = new ArrayList<Fragment>();
public VpAdapter(FragmentManager fm) {
super(fm);
}
public VpAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
}
<android.support.v4.view.ViewPager
android:id="@+id/one_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
fragment1对应的类Class One:
public class One extends Fragment {
private List<Fragment> list = new ArrayList<Fragment>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.one, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager vp = (ViewPager) getView().findViewById(R.id.one_vp);
list.add(new FragOne());//子fragment
list.add(new FragTwo());//子fragment
VpAdapter adapter = new VpAdapter(getChildFragmentManager(), list);
vp.setAdapter(adapter);
}
}
漏掉VpAdapter类了:
public class VpAdapter extends FragmentPagerAdapter {
private List<Fragment> list = new ArrayList<Fragment>();
public VpAdapter(FragmentManager fm) {
super(fm);
}
public VpAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询