2 myc 100 myc_100 于 2013.09.24 14:31 提问

隐藏和显示 Fragment 时显示错误的 fragment

第一次我把程序中的fragments的可见性改变了, fragment显示的不是我设置的。
My layout:

    <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"/>
   </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方法执行后,fragment显示的是 TwitterFragment,而不是我在onCreate方法的最后设置的StreamingFragment 。

2个回答

wang84100
wang84100   2016.06.11 15:34

我得布局跟你的类似, android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_bottom">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.hsj.messages.fragment.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_main" />

    <fragment
        android:id="@+id/msglistFragment"
        android:name="com.hsj.messages.fragment.MsgListFragment"
        android:layout_width="match_parent"
        tools:layout="@layout/fragment_msglist"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/settingFragment"
        android:name="com.hsj.messages.fragment.SettingFragment"
        android:layout_width="match_parent"
        tools:layout="@layout/fragment_setting"
        android:layout_height="match_parent" />
</RelativeLayout>

    显示的也有一些问题
    java代码如下
    mainFragment = (MainFragment) manager.findFragmentById(R.id.mainFragment);
    msgListFragment = (MsgListFragment) manager.findFragmentById(R.id.msglistFragment);
    settingFragment = (SettingFragment) manager.findFragmentById(R.id.settingFragment);

            fragmentTransaction.show(mainFragment);
    fragmentTransaction.hide(msgListFragment);
    fragmentTransaction.hide(settingFragment);
            显示的是最后一个fragment
wang84100
wang84100   2016.06.11 15:39

不知道你解决了没有,我得刚解决 主要commit();

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!