2015-01-16 35 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