嵌套的ViewPager不会重新加载标签页

时间:2018-11-28 18:05:31

标签: android android-fragments android-viewpager

我有: 1)包含ViewPager / TabLayout的主要活动。在每个选项卡中都有一个片段。 2)第二个选项卡/片段包含另一个ViewPager / TabLayout(如下图所示)。当然,这些页面中的每一个都是单独的另一个片段。

因此,当用户转到另一个标签时,我需要保持主观看者的刷新。问题是,第一次显示此第二个选项卡时,它可以正常工作,但是,当我第二次显示该选项卡时,似乎TabLayout没有选项卡(我可以滑动但不能固定在任何文本上)(请参见图2) 。当然,也没有内容显示。

这是适配器的代码。如果需要更多代码,我将进行更新。

public class PeopleFragment extends BaseFragment {

View root_view;

private ViewPager viewPager;
private TabLayout tabLayout;

private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_people, container, false);
    } catch (InflateException e) {
        /* fragment is already there, just return view as it is */
    }

    root_view = view;

    initUI(view);
    initEvent(view);

    return root_view;
}

@Override
void initUI(@Nullable final View view){
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.ppl_tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Friends"));
    tabLayout.addTab(tabLayout.newTab().setText("All Users"));
    //tabLayout.addTab(tabLayout.newTab().setText("Ranking"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    this.tabLayout = tabLayout;

    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.ppl_viewpager);
    final PagerPeopleAdapter adapter = new PagerPeopleAdapter
            (getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(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) {

        }
    });
    this.viewPager = viewPager;
}

@Override
void initEvent(@Nullable final View view){
    // Do nothing
}

@Subscribe
public void onDestroyView(){
    viewPager.invalidate();
    super.onDestroyView();
}

@Subscribe
public void onConfirmedFriendsReceived(ConfirmedFriendsEvent event){
    // Do nothing
}

enter image description here

enter image description here

0 个答案:

没有答案