当前位置:  开发笔记 > Android > 正文

如何在TabLayout和ViewPager中将片段替换为另一个片段

如何解决《如何在TabLayout和ViewPager中将片段替换为另一个片段》经验,为你挑选了0个好方法。

我想将片段替换为TabLayout Viewpager包含的另一个片段.像这样:

活动 - > 片段 - > B片段(TabLayout ViewPager) - > C片段(Tab片段) - > D片段

我的任务就是转C片段片段d.

注意: - TabLayout ViewPager还包含3个选项卡Fragment控件FragmentPagerAdapter.

Tablayout片段的布局:








    

片段B类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_select_postpaid_service, container, false);

    tablayout = (TabLayout) view.findViewById(R.id.tablayout_postpaid_service);
    viewpager = (ViewPager) view.findViewById(R.id.viewpager_postpaid_service);
    viewpager.setAdapter(new TabPagerAdapter(getChildFragmentManager(), TabConstants.POSTPAID_SERVICE_TYPE_TABS_FLAG, TabConstants.POSTPAID_SERVICE_TYPE_TABS));


    tablayout.post(new Runnable() {

        @Override
        public void run() {
            tablayout.setupWithViewPager(viewpager);
            setupTabIcons();
        }
    });
    return view;
}

片段C类:

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

    view = inflater.inflate(R.layout.fragment_select_operator, container,
            false);
    setGridview();
    return view;
}
private void setGridview() {

    operatorName = new ArrayList();
    operatorCode = new ArrayList();

    recyclerview = (RecyclerView) view
            .findViewById(R.id.select_operator_recycler_view);
    recyclerview.setHasFixedSize(true);
    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),
            Constants.NO_OF_OPERATOR_GRID);
    recyclerview.setLayoutManager(layoutManager);
    OperatorGridAdapter adapter = new OperatorGridAdapter(getActivity(),
            HomeConstants.OPERATOR_LIST_ICON);
    recyclerview.setAdapter(adapter);
    recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(
            getActivity(),
            new RecyclerItemClickListener.OnItemClickListener() {

                @Override
                public void onItemClick(View view, int position) {
                    navigateFragment(position);
                }

            }));
}
private void navigateFragment(int position) {

    Fragment fragment = new DFragment();

    // FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container_body, fragment);
     //fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commit();
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(
            getResources().getString(R.string.app_name));
    Log.d(Constants.ACTIVITY_TAG, "********************");
}


我收到一个错误

java.lang.IllegalArgumentException: No view found for id 0x7f0a006e (com.recharge:id/container_body) for fragment DFragment{4327cfd0 #2 id=0x7f0a006e}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)

我已经尝试了许多刻度和解决方案,但没有打我.
请帮我解决这个问题.

推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有