片段到片段交易android

时间:2015-01-16 03:55:29

标签: android android-fragments fragment

我有一个容器a.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tc.MainActivity"
    tools:ignore="MergeRootFrame" />

显示标签内容,即列表视图。第一个选项卡A是b.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


     <ListView
              android:id="@+id/list"
              android:layout_height="0dp"
              android:layout_width="match_parent"
              android:background="@color/lightGrey"
              android:layout_weight="0.8"
              android:divider="@android:color/transparent"
              android:dividerHeight="5.0sp"
              android:focusable="false"
              android:focusableInTouchMode="false">
         </ListView>

</LinearLayout>

在A内部,当用户点击单个列表视图项时,我想用另一个片段替换当前列表视图,即ContentFragment。

    public void onItemClick(AdapterView<?> parent, final View view,
            int position, long id) {
        ContentFragment c= new ContentFragment();
        c.setArguments(args);
        a.getFragmentManager().beginTransaction()
        .replace(xxx, c)
        .addToBackStack(null)
        .commit();
    }

对于xxx字段,我想我应该填写a.container?但我怎么能得到它?在标签A的内部,我是inflater.inflate(R.layout.b,container,false);

1 个答案:

答案 0 :(得分:0)

你可以在tabview中替换这样的片段

 public void replaceFragment(Fragment fragment,booleanaddToBackStack,ContextmContext) 
 {
            FragmentTransaction transaction = ((FragmentActivity) mContext).getFragmentManager().beginTransaction();
            if (addToBackStack) 
            {
                transaction.addToBackStack(fragment.getClass().getSimpleName());
            }


            transaction.add(R.id.realtabcontent, fragment);
            transaction.commit();
            ((FragmentActivity) mContext).getFragmentManager().executePendingTransactions();

 }