简体   繁体   English

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

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

I want to replace fragment to another fragment which contain by TabLayout Viewpager. 我想将片段替换为TabLayout Viewpager包含的另一个片段。 like this: 像这样:

Activity -> A Fragment -> B Fragment (TabLayout ViewPager) -> C Fragment (Tab's Fragment) -> D Fragment 活动 - > 片段 - > B片段 (TabLayout ViewPager) - > C片段 (Tab片段) - > D片段

I task is switch Fragment C to Fragment D . 我的任务是将片段C切换到片段D.

Note:- TabLayout ViewPager also contain 3 tab Fragment control by FragmentPagerAdapter . 注: - TabLayout ViewPager还包含了3片,分段控制FragmentPagerAdapter

Tablayout fragment's layout: 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>

Fragment B class: 片段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;
}

Fragment C class: 片段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, "********************");
}


I getting an error 我收到一个错误

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)

I've tried many tick and solution but not hit for me . 我已经尝试了许多刻度和解决方案,但没有打我。
Please help me short out this problem. 请帮我解决这个问题。

Have you tried? 你有没有尝试过?

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

and I think the error was quite obvious. 我认为这个错误非常明显。 The compile says: 编译说:

not view found for container_body

And in your code: 在你的代码中:

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

} }

I see that you try to use fragmentTransaction.replace(R.id.container_body, fragment); 我看到你尝试使用fragmentTransaction.replace(R.id.container_body,fragment); However you just comment the container_body in your layout. 但是,您只需在布局中注释container_body即可 That's why the compile told you that it can't find container_body for the fragment to inflate. 这就是为什么编译告诉你它无法找到容器膨胀的容器 Maybe that's the problem? 也许那就是问题?

Also it should be helpful if you could post more details about your layout for fragment D. 如果您可以发布有关片段D的布局的更多详细信息,它也应该会有所帮助。

Let me know if this solve your problem ;) 如果这可以解决您的问题,请告诉我;)

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

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