在这里发生了一些奇怪的事情,当我从第一个标签页转到第二个标签时,我创建了一个带有查看寻呼机的RSS Feed应用第3个内容标签即将进入第二个标签。当我从第二个标签转到第一个标签时,第一个标签的内容将进入第二个标签。没有任何逻辑混合的东西(或者我找不到逻辑),为什么会发生......?有没有办法在转到下一个标签之前清除当前标签?(不知道这是我应该问的正确问题)
这是我的代码
public class datafragment extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.sample,container,false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(new sliderAdapter(getChildFragmentManager()));
tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
private class sliderAdapter extends FragmentPagerAdapter{
final String tabs[]={"tab1", "tab2","tab3"};
public sliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment= null;
switch (position){
case 0:
fragment = new RssFragment();
break;
case 1:
fragment = new RssFragment1();
break;
case 2:
fragment = new RssFragment2();
break;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
添加答案后
public class datafragment extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.sample,container,false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(new SliderAdapter(getChildFragmentManager()));
tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
// tabLayout.post(new Runnable() {
// @Override
// public void run() {
// tabLayout.setupWithViewPager(viewPager);
// }
// });
**setupViewPager(viewPager);**
return view;
}
private void setupViewPager(ViewPager viewPagerProfile)
{
SliderAdapter adapter = new SliderAdapter (getActivity().getSupportFragmentManager());
adapter.addFragment(new RssFragment(), "tab1");
adapter.addFragment(new RssFragment1(), "tab2");
adapter.addFragment(new RssFragment2(), "tab3");
viewPagerProfile.setAdapter(adapter);
}
public class SliderAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SliderAdapter (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 null;
}
}
}
答案 0 :(得分:0)
尝试这种方式:
private void setupViewPager(ViewPager viewPagerProfile)
{
SliderAdapter adapter = new SliderAdapter (getSupportFragmentManager());
adapter.addFragment(new RssFragment(), "tab1");
adapter.addFragment(new RssFragment1(), "tab2");
adapter.addFragment(new RssFragment2(), "tab3");
viewPagerProfile.setAdapter(adapter);
}
自定义适配器:
public class SliderAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SliderAdapter (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 null;
}
}
如果您想对选定的片段执行某些操作,请添加listenre:
TabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override public void onTabSelected(TabLayout.Tab tab)
{
//Code here for selected fragment
}
@Override public void onTabUnselected(TabLayout.Tab tab)
{
}
@Override public void onTabReselected(TabLayout.Tab tab)
{
}
});