2013-10-14 29 views
0

我在我的MainActivity的佈局中有FrameLayout元素,多種類型的Fragments用作「連接點」。Android碎片替換自己的根視圖

查看:

<FrameLayout 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

片段取代:

Fragment fragment = new CoursesFragment(); 
getFragmentManager().beginTransaction().replace(R.id.main_content, fragment).commit(); 

現在我在我需要直接從CoursesFragment改變main_content(片段取代了自己的看法)的情況。什麼是最好的方法來做到這一點?它安全嗎?我可以達到MainActivity的上下文並更改它,但我不知道它是否正確。

回答

2

我不知道你的View是什麼,但最簡單的方法是在他們自己的ViewGroup中提供兩個視圖,同時在setVisibility(View.GONE)上設置'其他視圖'。如果您需要切換,只需將顯示的第一個視圖設置爲View.GONE,第二個設置爲View.VISIBLE

這樣您就不必使用活動的上下文,或片段管理器hideshow方法。

+0

我有兩個以上的片段(視圖),這是它們之間的尷尬切換。 – skywall