我对TabLayout
与ViewPager
有一些疑问。我已经在RecyclerView
的片段中实现了Tablayout
。此TabLayout
在导航抽屉项目的Fragment
上实现。
当我单击TabLayout
打开的导航抽屉片段视图时,RecyclerView的项目也会在第一次显示。但是,第二次单击NevigationDrawer
,则不会显示RecyclerView的项目。为什么...?
public class DashBoardRequest extends Fragment implements TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
private ViewPager viewPager;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_dash_board_request, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tab);
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
setupViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
tabLayout.setTabTextColors(ContextCompat.getColorStateList(getContext(), R.color.colorPrimaryDark));
return view;
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) { }
@Override
public void onTabReselected(TabLayout.Tab tab) { }
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new AcceptedClientClass(), "Accepted");
adapter.addFragment(new RejectedClientClass(), "Rejected");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
答案 0 :(得分:1)
您可以为此使用 FragmentStatePagerAdapter 或使用 viewpager。 setOffscreenPageLimit()。
答案 1 :(得分:0)
我使用以下方法解决了我的问题:-
已解决:-使用FragmentPagerAdapter中的FragmentStatePagerAdapter替换
公共类DashBoardRequest扩展了Fragment实现TabLayout.OnTabSelectedListener {
private TabLayout tabLayout;
private ViewPager viewPager;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_dash_board_request, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tab);
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
setupViewPager(viewPager);
viewPager.setOffscreenPageLimit(1);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
tabLayout.setTabTextColors(ContextCompat.getColorStateList(getContext(), R.color.colorPrimaryDark));
return view;
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) { }
@Override
public void onTabReselected(TabLayout.Tab tab) { }
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new AcceptedClientClass(), "Accepted");
adapter.addFragment(new RejectedClientClass(), "Rejected");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}