Fragment中的ViewPager,只有选项卡才能在第一个视图上加载选项卡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fragment中的ViewPager,只有选项卡才能在第一个视图上加载选项卡相关的知识,希望对你有一定的参考价值。
我有一个带有选项卡的片段,它使用了一个视图寻呼机。问题是当我第一次访问片段时会出现选项卡,但如果我导航到其他地方然后返回到此片段,则视图寻呼机中的片段不再出现。
我使用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
答案
处理片段生命周期实现覆盖方法如下:
@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);
}
以上是关于Fragment中的ViewPager,只有选项卡才能在第一个视图上加载选项卡的主要内容,如果未能解决你的问题,请参考以下文章