用新片段替换ListFragment项目中的占位符

时间:2015-02-02 17:56:48

标签: android android-fragments

我有分类列表的片段。此列表中的项目如下所示。我需要在此列表的元素上单击以通过将占位符替换为CategoryListFragment的新实例来显示子类别。如何使用FragmentManager替换那个场景?

layout.xml

<?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>

片段

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()...
        // ???
    }

}

0 个答案:

没有答案