6

Ich möchte Fragment zu einem anderen Fragment ersetzen, die von TabLayout Viewpager enthalten. dies wie:So ersetzen Sie ein Fragment zu einem anderen Fragment in TabLayout und ViewPager

Aktivität ->Ein Fragment ->B Fragment (TabLayout ViewPager) ->C Fragment (Tab das Fragment) ->D Fragment

I Aufgabe ist Schalter Fragment C bis Fragment D.

Hinweis: - TabLayout ViewPager enthält auch 3 Registerkarte Fragment control von FragmentPagerAdapter.

Tablayout Fragment 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-Klasse:

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

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


ich eine E bekommen rror

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) 

Ich habe viele Tick und Lösung versucht, aber nicht für mich getroffen.
Bitte helfen Sie mir, dieses Problem kurz zu machen.

+0

Soweit ich verstehe, Sie versuchen, zu erstellen verschachteltes Tab-Layout –

+0

@Gaurav Balbhadra kann sagen ...... eigentlich möchte ich Tab-Fragment (das durch TabLayout-Fragment enthält) mit einem anderen Fragment ändern. –

+0

überprüfen Sie dies, es könnte Ihnen helfen http://StackOverflow.com/Questions/34261057/android-how-to-build-multiplevel-Tabs/34262629#34262629 –

Antwort

0

Haben Sie es versucht?

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

codiert in Fragment A ...... ja! Ich habe es getan –

+0

aber kein Treffer ..... :( –

+1

Hallo Garg, wie wir Tab-Layout mit Ansicht Pager verwenden, gibt es keine Rahmen-Layout in der Activity XML-Datei. Wenn Sie damit fertig sind, können Sie erkläre die main_container id? – sarankumar

0

und ich denke, der Fehler war ziemlich offensichtlich. Die Kompilierung sagt:

not view found for container_body 

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

}

Ich sehe, dass Sie fragmentTransaction.replace (R.id.container_body, Fragment) zu verwenden versuchen; Allerdings kommentieren Sie einfach die container_body in Ihrem Layout. Deshalb hat Ihnen die Kompilation gesagt, dass sie container_body nicht finden kann, damit das Fragment aufgebläht wird. Vielleicht ist das das Problem?

Auch sollte es hilfreich, wenn Sie genauere Informationen über das Layout für Fragment D.

Lassen Sie mich schreiben konnte wissen, ob dies Ihr Problem lösen;)

+0

liebe diesen Code kommentiert b'coz framelayout Komponente ist nicht geeignet an dieser Position Ursache für eine andere Ausnahme ...... das ist meine eigentliche Frage "wie Fragment in tablayout Fragment ersetzen" –