我有3个片段,首先是mainFragment
,tab1Fragment
和Detaitab1Fragment
我想在 tab1Fragment 中调用 Detaitab1Fragment 。
当我在tab1Fragment
中调用我的Detaitab1Fragment时,这是代码 BlankFragment fragment = new BlankFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
这段代码我在mainFragment上的SectionPageAdapter
private void setupViewPager(ViewPager viewPager){
NotifikasiFragment.SectionPageAdapter adapter = new NotifikasiFragment.SectionPageAdapter(getChildFragmentManager());
adapter.addFragment(new TabKelasNotifikasiFragment(),"Kelas");
adapter.addFragment(new TabSekolahNotifikasiFragment(),"Sekolah");
viewPager.setAdapter(adapter);
}
public class SectionPageAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mfragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mfragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mfragmentList.get(position);
}
@Override
public int getCount() {
return mfragmentList.size();
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
这是我在mainFragment上调用viewPager时的代码
SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getChildFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.containerNotifikasi);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabsNotifikasi);
tabLayout.setupWithViewPager(viewPager);
下面的代码是我的mainFragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fania.suara_project.fragment.notifikasi.NotifikasiFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#000000"
android:text="NOTIFIKASI"
android:textSize="20dp"
android:layout_below="@+id/text_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></TextView>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_below="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabsNotifikasi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/containerNotifikasi"
android:layout_below="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
和代码tab1Fragment.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fania.suara_project.fragment.notifikasi.TabKelasNotifikasiFragment">
<android.support.v7.widget.RecyclerView
android:background="@color/kuning"
android:id="@+id/recycleViewKelasNotifikasi"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
我的FrameLayout位于content_drawable.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.fania.suara_project.activity.DrawableActivity"
tools:showIn="@layout/app_bar_drawable">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
我在Drawableactivity.class上调用content_drawable
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
R.id.drawable_layout包含content_drawable.xml
但我发现这样的错误
java.lang.IllegalArgumentException: No view found for id 0x7f0e0099 (com.example.fania.suara_project:id/frame) for fragment BlankFragment{9df0d4d #0 id=0x7f0e0099}
谁能帮帮我?
感谢的
答案 0 :(得分:1)
据我了解,你的片段属于同一个活动,对吧?请尝试拨打getActivity().getFragmentManager()
(或getActivity().getSupportFragmentManager()
)而不是getChildFragmentManager()
。
id =“frame”的容器在你的活动的xml中?