繁体   English   中英

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

[英]How to replace fragment to another fragment in TabLayout and ViewPager

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

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

我的任务是将片段C切换到片段D.

注: - TabLayout ViewPager还包含了3片,分段控制FragmentPagerAdapter

Tablayout片段的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<android.support.design.widget.TabLayout
    android:id="@+id/tablayout_postpaid_service"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/white"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/white"
    app:tabTextColor="@color/tab_fade_text_color" >
</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager_postpaid_service"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" /> -->
</android.support.v4.view.ViewPager>

片段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<String>();
    operatorCode = new ArrayList<String>();

    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)

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

你有没有尝试过?

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new FragmentB()).commit();

我认为这个错误非常明显。 编译说:

not view found for container_body

在你的代码中:

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, "********************");

}

我看到你尝试使用fragmentTransaction.replace(R.id.container_body,fragment); 但是,您只需在布局中注释container_body即可 这就是为什么编译告诉你它无法找到容器膨胀的容器 也许那就是问题?

如果您可以发布有关片段D的布局的更多详细信息,它也应该会有所帮助。

如果这可以解决您的问题,请告诉我;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM