2011-06-04 82 views
3

片段更新我的用新片段的Android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayout13" android:layout_height="wrap_content" android:paddingRight="70dp" android:orientation="vertical" android:paddingLeft="70dp" android:layout_width="wrap_content"> 
      <ImageView android:id="@+id/baby_icon" android:layout_height="wrap_content" android:src="@drawable/baby" android:clickable="true" android:layout_width="wrap_content"></ImageView> 
     </LinearLayout> 
    </LinearLayout> 
    <fragment 
     android:name="com.nicu.health.FragAFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/yellow_cardlist_fragment" 
     android:layout_weight="1" 
     > 
    </fragment> 
</LinearLayout> 

布局在启动这确实显示正确的片段(FragAFragment)。现在按钮点击baby_icon我尝试删除当前片段并添加一个新的(FragBFragment),它具有完全不同的布局。

虽然我看到onCreateView方法被调用,它确实返回一个非空视图,但新片段屏幕上的UI没有更新。我使用下面的代码来更新片段。

     Fragment baby = FragBFragment.newInstance(1); 
        FragmentTransaction ft = getFragmentManager().beginTransaction(); 
//     ft.remove(getFragmentManager().findFragmentById(R.id.yellow_cardlist_fragment)); 
//     ft.add(R.id.yellow_cardlist_fragment, baby); 
        ft.replace(R.id.yellow_cardlist_fragment, baby); 
        ft.addToBackStack(null); 
        Log.i(TAG, "Code for commit = "+ft.commit()); 

我已经尝试删除,替换和添加的所有组合,以获得片段的事情,但徒劳无功!

而且还我试图与代码

<fragment 
    android:name="com.nicu.health.FragBFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/yellow_cardlist_fragment" 
    android:layout_weight="1" 
    > 

,这是否工作在启动时显示的第二个片段!!!!

帮助将reeally appreicated。

感谢,

回答

0

好吧,我的第一个猜测:你忘了说ft.commit();

我的第二个猜测:

比方说u有活动AB控制片段A和片段B.

您也有3个布局。 Layout_ActivityAB,Layout_FragA,Layout_FragB。

因此,在Activity AB中调用setContentView(R.layout.Layout_ActivityAB)。这将是你告诉android在哪里放置片段的布局。

内部片段A或片段B:

@Override 
    //onCreateView is called when an instance of this class is first created. 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.Layout_FragA, container, false); return rootView; 
    } 

R.layout.Layout_FragA被称为片段的内部。 Layout_FragA是片段的实际内容。