2015-01-16 152 views
0

我有一個容器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" /> 

它是顯示選項卡的內容,這是一個列表視圖。第一個標籤一個是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> 

內的,當個人的ListView項用戶點擊,我想與另一個片段即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);

+0

取代片段這樣嗎? –

+1

這是你的活動,應該改變片段。在你的Fragment中,你可以用getActivity()調用活動,然後請求更改Fragment –

+0

R.id.container是錯誤的,因爲我的標籤A的佈局文件(b.xml)沒有id:container,它存在於,xml。 @Arthur_gg,a.getFragmentManager()。beginTransaction()。replace正在做片段更改,其中a是活動。 – Hammer

回答

0

你可以得到它使用`R.id.container`在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(); 

} 
+0

其實我以前試過類似的東西,而且片段沒有被替換。 。\t \t \t a.getFragmentManager()的BeginTransaction() 。新增(C, 「commentList」) //該事務添加到返回堆疊 .addToBackStack(空) .commit(); \t \t \t \t a.getFragmentManager()。executePendingTransactions(); – Hammer

+0

你需要把標籤內容id transaction.add(R.id.realtabcontent,fragment); – Ram

+0

在我的情況下,它是R.id.list,它不工作以及 – Hammer