How to replace fragment to another fragment in TabLayout and ViewPager
Asked Answered
A

2

6

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.

Abstruse answered 18/12, 2015 at 14:40 Comment(4)
As far as i understand, you are trying to create nested Tab layout.Thorner
@Gaurav Balbhadra can say...... actually i want to change Tab fragment (which containing by TabLayout's fragment) with another fragment.Abstruse
check out this it might help you #34261557Thorner
@Gaurav Balbhadra no luck......Abstruse
A
0

Have you tried?

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new FragmentB()).commit();
Annatto answered 18/12, 2015 at 15:3 Comment(1)
Hello Garg's, As we are using tab layout with view pager, there is no frame layout in the Activity xml file. If you are done with this, can you explain the main_container id?Anole
A
0

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 ;)

Alphonsa answered 18/12, 2015 at 15:25 Comment(1)
dear that code commented b'coz framelayout component is not suitable at this position it cause of one another exception ...... thats is my actual Question "how to replace fragment in tablayout's fragment". IAbstruse

© 2022 - 2024 — McMap. All rights reserved.