我第一次更改应用中片段的可见性时,片段显示的不是我设置的片段。
我的布局:
<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,而不是我在onCreate方法的最后设置中显示的StreamingFragment。
答案 0 :(得分:4)
我发现问题,我改为隐藏onCreate中的所有片段。 Aparentelly在xml中的android:visibility =“gone”没有做到这一点。
以下是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 :(得分:1)
我在2个按钮(button1和button2)上遇到了同样的问题,当我不得不隐藏第一个按钮时,第二个隐藏了,第一个没有做任何事情......我所做的一切都是CTRL + C xml和CTRL + V回来......希望它有效......