我想将片段替换为TabLayout Viewpager包含的另一个片段。像这样:
活动 - > 片段 - > B片段(TabLayout ViewPager) - > C片段(标签片段) - > D片段
我的任务是将 Fragment C 切换到 Fragment D 。
注意: - TabLayout ViewPager还包含FragmentPagerAdapter
的3个标签片段控件。
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)
我尝试了很多刻度和解决方案,但没有打我。
请帮我解决这个问题。
答案 0 :(得分:0)
你试过吗?
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new FragmentB()).commit();
答案 1 :(得分:0)
我觉得这个错误非常明显。编译说:
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 。这就是为什么编译告诉你它无法找到 container_body 来让片段膨胀。也许这就是问题?
如果您可以发布有关片段D的布局的更多详细信息,也应该会有所帮助。
如果这可以解决您的问题,请告诉我;)