我有一个带有选项卡的片段,其中使用了一个视图分页器。问题是,当我第一次访问该片段时会出现选项卡,但是如果我导航到其他地方然后返回到该片段,则视图分页器中的片段将不再出现。
我使用SectionsPagerAdapter如下:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
//ClientHealthRootFragment tab1 = new ClientHealthRootFragment();
ChatListFragment tab1 = new ChatListFragment();
return tab1;
case 1:
ClientProfileFragment tab2 = new ClientProfileFragment();
//ChatListFragment tab2 = new ChatListFragment();
return tab2;
case 2:
//ClientCommentRootFragment tab3 = new ClientCommentRootFragment();
ChatListFragment tab3 = new ChatListFragment();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Health";
case 1:
return "Profile";
case 2:
return "Comments";
}
return null;
}}
然后在我处理这些部分的片段中是以下内容:
public class ClientMainFragment extends Fragment {
private ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
String client_name;
int client_image;
private TextView clientNameText;
private ImageView clientImage;
public ClientMainFragment() {
//empty constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_client_main, container, false);
clientNameText = view.findViewById(R.id.clientNameTxt);
clientImage = view.findViewById(R.id.clientImage);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager);
return view;
}}
我不确定我缺少什么,我们将不胜感激。
编辑:解决方案
我通过以下链接找出了问题所在: Tab content disappeared after change page
答案 0 :(得分:0)
处理片段生命周期实施ovverride方法如下:
@Override
public void onResume() {
super.onResume();
}
,然后在onReume方法中将viewpager代码的Hole Code如下所示:
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager);
因此最终代码将为:
@Override
public void onResume() {
super.onResume();
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager);
}