重新加载后不显示选项卡片段

时间:2018-06-20 11:35:33

标签: android

我正在使用视图分页器和标签创建应用程序:

第一次单击底部导航中的“菜单”项,通过单击选项卡并滑动(viewpager),将在屏幕上按预期显示文本frag1,frag2和frag3。

请参见下图:

enter image description here

但是,当我单击另一个项目(例如家庭)并返回“菜单”项目时,文本片段1等不会显示。参见下图:enter image description here

带有viewpageradapter的MenuFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container, false);
    setHasOptionsMenu(true);
    ButterKnife.bind(this, view);

    FragmentManager fragManager = myContext.getSupportFragmentManager();
    viewPagerAdapter = new ViewPagerAdapter(fragManager);

    viewPagerAdapter.addFragment(new MenuRecyclerViewFragment(), "FRAG1");
    viewPagerAdapter.addFragment(new MenuRecyclerViewFragment(), "FRAG2");
    viewPagerAdapter.addFragment(new MenuRecyclerViewFragment(), "FRAG3");

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

    viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @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) {

        }
    });

    return view;

}

class ViewPagerAdapter extends FragmentPagerAdapter {
    public final List<Fragment> mFragmentList = new ArrayList<>();
    public final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragmentList.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("text", mFragmentTitleList.get(position));
        fragment.setArguments(bundle);
        return fragment;
    }

    @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);
    }
}

MenuRecylerViewFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_menu_recycler_view, container, false);
    ButterKnife.bind(this, view);

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        String value = bundle.getString("text", "test");
        textView.setText(value);
    }


    return view;
}

3 个答案:

答案 0 :(得分:1)

您的ViewPagerAdapter类需要由FragmentStatePagerAdapter而不是FragmentPagerAdapter扩展。 查看FragmentStatePagerAdapter的更多详细信息。

答案 1 :(得分:0)

第二次登陆该片段,getArguments()不再不再为null。 改为使用savedInstanceState捆绑包。

答案 2 :(得分:0)

1.) 由 ViewPager 托管的片段应由 FragmentPagerAdapter.getItem() 创建。

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public ViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0) return new MenuRecyclerViewFragment();
        if(position == 1) return new MenuRecyclerViewFragment();
        if(position == 2) return new MenuRecyclerViewFragment();

        throw new IllegalStateException("Unexpected position " + position);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position == 0) return "FRAG1";
        if(position == 1) return "FRAG2";
        if(position == 2) return "FRAG3";
      
        throw new IllegalStateException("Unexpected position " + position);
    }
}

2.) Fragment 内的 ViewPagers(托管 Fragment)应该被赋予 getChildFragmentManager() 而不是 context.getSupportFragmentManager() 以使其工作。