简体   繁体   中英

How to replace fragment to another fragment in TabLayout and ViewPager

I want to replace fragment to another fragment which contain by TabLayout Viewpager. like this:

Activity -> A Fragment -> B Fragment (TabLayout ViewPager) -> C Fragment (Tab's Fragment) -> D Fragment

I task is switch Fragment C to Fragment D .

Note:- TabLayout ViewPager also contain 3 tab Fragment control by FragmentPagerAdapter .

Tablayout fragment's layout:

<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:

@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:

@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); However you just comment the container_body in your layout. 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.

Let me know if this solve your problem ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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