2013-08-06 137 views
0

在我第一次更改應用程序中片段的可見性時,片段顯示的不是我設置顯示的片段。Android - 隱藏/顯示片段顯示錯誤的片段

我的佈局:

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

<fragment 
    android:id="@+id/act_main_frag_top_bar" 
    class="br.com.myapp.fragments.TopBarFragment" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 

<fragment 
    android:id="@+id/act_main_frag_toolbar" 
    class="br.com.myapp.fragments.ToolbarFragment" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 

<!-- Tabs -->  
<fragment 
    android:id="@+id/act_main_frag_twitter" 
    class="br.com.myapp.fragments.TwitterFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:visibility="gone"/> 

<fragment 
    android:id="@+id/act_main_frag_facebook" 
    class="br.com.myapp.fragments.FacebookFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:visibility="gone"/> 

<fragment 
    android:id="@+id/act_main_frag_tv_show" 
    class="br.com.myapp.fragments.TVShowFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:visibility="gone"/> 

<fragment 
    android:id="@+id/act_main_frag_info" 
    class="br.com.bluepen.mixtv.fragments.InfoFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:visibility="gone"/> 

<fragment 
    android:id="@+id/act_main_frag_streaming" 
    class="br.com.myapp.fragments.StreamingFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:visibility="gone"/> 

</LinearLayout> 

而且myActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FragmentManager manager = getSupportFragmentManager(); 
    mStreamingFrag = (StreamingFragment) manager.findFragmentById(R.id.act_main_frag_streaming); 
    mFacebookFrag = (FacebookFragment) manager.findFragmentById(R.id.act_main_frag_facebook); 
    mTwitterFrag = (TwitterFragment) manager.findFragmentById(R.id.act_main_frag_twitter); 
    mTVShowFrag = (TVShowFragment) manager.findFragmentById(R.id.act_main_frag_tv_show); 
    mInfoFrag = (InfoFragment) manager.findFragmentById(R.id.act_main_frag_info); 
    mToolbar = (ToolbarFragment) manager.findFragmentById(R.id.act_main_frag_toolbar); 
    mToolbar.setListener(this); 

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.show(mStreamingFrag); 
    transaction.commit(); 
} 

中的onCreate後,片段越來越表現爲TwitterFragment,而不是StreamingFragment我設置在最後的展現onCreate方法。

回答

4

我發現這個問題,我改變了隱藏onCreate中的所有片段。 Aparentelly的android:可見性=「走了」在xml沒有這樣做。

這裏是的onCreate的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction transaction = manager.beginTransaction(); 

    mStreamingFrag = (StreamingFragment) manager.findFragmentById(R.id.act_main_frag_streaming); 
    transaction.show(mStreamingFrag); 
    mFacebookFrag = (FacebookFragment) manager.findFragmentById(R.id.act_main_frag_facebook); 
    transaction.hide(mFacebookFrag); 
    mTwitterFrag = (TwitterFragment) manager.findFragmentById(R.id.act_main_frag_twitter); 
    transaction.hide(mTwitterFrag); 
    mTVShowFrag = (TVShowFragment) manager.findFragmentById(R.id.act_main_frag_tv_show); 
    transaction.hide(mTVShowFrag); 
    mInfoFrag = (InfoFragment) manager.findFragmentById(R.id.act_main_frag_info); 
    transaction.hide(mInfoFrag); 

    transaction.commit(); 
} 

我不`噸知道我是否可以設置在XML隱藏片段。

1

我有2個按鈕(button1和button2)相同的問題,當我不得不隱藏第一個按鈕,第二個隱藏,第一個什麼也沒做......我做的一切都是CTRL + C xml和CTRL + V回來...希望它的作品...

+0

我找到了問題。檢查我的答案吼叫。 – jonathanrz

+0

對不起,我之前沒有看到......這幾乎就是我在說的! :-) – user2606414