public class Fragment01 extends Fragment{
private TabLayout tablayout;
private ViewPager viewpager;
private List<String> list;
private LayoutInflater mInflater;
private WeakReference<View> mRootView = null;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (inflater != null)
mInflater = inflater;
else
mInflater = LayoutInflater.from(getActivity());
View rootView = mRootView == null ? null : mRootView.get();
if (rootView != null) {
final ViewParent parent = rootView.getParent();
if (parent != null && parent instanceof ViewGroup)
((ViewGroup) parent).removeView(rootView);
}
else {
rootView = mInflater.inflate(R.layout.fragment01, null, false);
mRootView = new WeakReference<View>(rootView);
}
tablayout=rootView.findViewById(R.id.tablayout);
viewpager=rootView.findViewById(R.id.viewpager);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list=new ArrayList<>();
list.add("推荐");
list.add("课程");
list.add("实战");
list.add("职业路径");
viewpager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position){
case 0:
fragment=new Fragment_tuijian();
break;
case 1:
fragment=new Fragment_kecheng();
break;
case 2:
fragment=new Fragment_shizhan();
break;
case 3:
fragment=new Fragment_zhiyelujing();
break;
}
return fragment;
}
@Override
public int getCount() {
return list.size();
}
});
//设置tab和viewpager关联
tablayout.setupWithViewPager(viewpager);
}
}
private TabLayout tablayout;
private ViewPager viewpager;
private List<String> list;
private LayoutInflater mInflater;
private WeakReference<View> mRootView = null;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (inflater != null)
mInflater = inflater;
else
mInflater = LayoutInflater.from(getActivity());
View rootView = mRootView == null ? null : mRootView.get();
if (rootView != null) {
final ViewParent parent = rootView.getParent();
if (parent != null && parent instanceof ViewGroup)
((ViewGroup) parent).removeView(rootView);
}
else {
rootView = mInflater.inflate(R.layout.fragment01, null, false);
mRootView = new WeakReference<View>(rootView);
}
tablayout=rootView.findViewById(R.id.tablayout);
viewpager=rootView.findViewById(R.id.viewpager);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list=new ArrayList<>();
list.add("推荐");
list.add("课程");
list.add("实战");
list.add("职业路径");
viewpager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position){
case 0:
fragment=new Fragment_tuijian();
break;
case 1:
fragment=new Fragment_kecheng();
break;
case 2:
fragment=new Fragment_shizhan();
break;
case 3:
fragment=new Fragment_zhiyelujing();
break;
}
return fragment;
}
@Override
public int getCount() {
return list.size();
}
});
//设置tab和viewpager关联
tablayout.setupWithViewPager(viewpager);
}
}