2017-06-16 102 views
0

我注意到我的片段之一的onCreateView()永遠不會被調用,我不知道爲什麼。我需要能夠在onResume()中進行更改,但那也永遠不會被調用。片段onCreateView永遠不會被調用

這是我如何添加片段。

public class LiveGameStats extends FragmentActivity { 

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

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit(); 
    } 

    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost); 
    mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent); 
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"), 
      LiveGameTab1Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"), 
      LiveGameTab2Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"), 
      LiveGameTab3Fragment.class, null); 
} 

Tab1Fragment

​​

而且finnaly的XML R.layout.tab1

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".DeviceFragment" > 

    <ImageButton 
    android:id="@+id/searchBtn" 
    android:src="@drawable/waves" 
    android:background="@drawable/search_selector" 
    android:padding="40dp" 
    android:layout_height="200dp" 
    android:layout_width="200dp" 
    android:onClick="liveMatch" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

下穿過我想我可能已經創建的片段走錯了路。正如你看到它現在他們創建爲XML說,但當我改變標籤,內容是「重置」。任何改變我放在onCreateView()onResume()不叫。

編輯

我試圖實現在第一註釋中所描述的方法。實施後,標籤具有相同的內容。就像所有的標籤都有相同的內容。這一次我發佈了所有的課程及其關聯的XML。

public class LiveGameStats extends FragmentActivity { 

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

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab2Fragment()).commit(); 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab3Fragment()).commit(); 

    } 

    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost); 
    mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent); 
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"), 
      LiveGameTab1Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"), 
      LiveGameTab2Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"), 
      LiveGameTab3Fragment.class, null); 
} 

activity_live_game_stats.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_live_game_stats" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_blue_light" 
    tools:context="lucian.leagueassistant.activity.LiveGameStats"> 


    <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/liveGameTabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TabWidget 
      android:id="@android:id/tabs" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 

     <FrameLayout 
      android:id="@+id/liverealtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 
    </android.support.v4.app.FragmentTabHost> 
</LinearLayout> 

我不會從LiveGameTab2LiveGameTab3職位的代碼,因爲他們幾乎是相同的。我也有

EDIT2

似乎它解決了,當我從getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();改爲getSupportFragmentManager().beginTransaction().add(android.R.id.tabcontent, new LiveGameTab1Fragment()).commit();

回答

1

問題的另一項活動是這樣的:

getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit(); 

在您需要的第一個參數將一個id傳遞給活動佈局中的一個容器,您的片段將被附加到該容器中。

您正在傳遞整個佈局R.id.tab1

添加容器視圖中activity_live_game_stats xml文件,

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".DeviceFragment" > 

    <FrameLayout 
    android:id="@+id/myContainer" 
    ...(Other attrs)> 
    </FrameLayout> 

</RelativeLayout> 

然後

if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().add(R.id.myContainer, new Tab1Fragment()).commit(); 
    } 
+0

我發佈了一個編輯,我試過你的方法。你可以看看它並張貼在這裏 – user2907139

相關問題