Android 用新片段替换ListFragment项中的占位符
我有一个带有类别列表的片段。此列表中的项目布局如下所示。我需要单击这个列表的元素,通过用CategoryListFragment的新实例替换占位符来显示子类别。如何在该场景中使用FragmentManager进行替换 layout.xmlAndroid 用新片段替换ListFragment项中的占位符,android,android-fragments,Android,Android Fragments,我有一个带有类别列表的片段。此列表中的项目布局如下所示。我需要单击这个列表的元素,通过用CategoryListFragment的新实例替换占位符来显示子类别。如何在该场景中使用FragmentManager进行替换 layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/category_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_below="@+id/category_item_name"
android:id="@+id/subcategories_fragment_placeholder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
getActivity().getFragmentManager()
?我正在使用嵌套片段,所以我认为getChildFragmentManager()
可以,但我不知道下一步是什么-如何在其中使用占位符。getChildFragmentManager().beginTransaction().replace(v.getId,new CategoryListFragment())代码>。如果您的适配器没有为每一行设置id,请通过调用view.setId(view.generateViewId())
谢谢:)自行完成。我不知道view.setId(view.generateViewId())
public class CategoryListFragment extends android.support.v4.app.ListFragment {
public static CategoryListFragment create(Category parent) {
CategoryListFragment f = new CategoryListFragment();
...
return f;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FrameLayout placeholder = (FrameLayout) v.findViewById(R.id.subcategories_fragment_placeholder);
CategoryListFragment subs = CategoryListFragment.create(adapter.getItem(position));
getChildFragmentManager().beginTransaction()...
// ???
}
}