0

在我的應用程序中,我在ActionBar中添加了一個DropDown Spinner,現在我無法做出 - 如何更改每個下拉選定的底部部分。在我的activity_main.xml佈局(Home)中,只包含一個顯示列表的Fragment。我正在尋找的是,創建其他UI屏幕也作爲片段,並更改放置片段選擇每dropDown。我的意思是主頁有fragment_main.xml,dropDown設置被選中,然後顯示setting_fragment.xml等等。我主要是:從ActionBar的dropDown調用其他片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" 
tools:ignore="MergeRootFrame" > 

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="terryconsulting.servicestation.AppointListFragment" 
    android:id="@+id/main_fragment" 
    /> 
</FrameLayout> 

在MainActivity類別,我已經實現ActionBar.OnNavigationListener其稱爲早期PlaceholderFragment,只是顯示在屏幕上的Hello World文本。

@Override 
public boolean onNavigationItemSelected(int position, long id) { 
    // When the given dropdown item is selected, show its contents in the 
    // container view. 
    //getFragmentManager().beginTransaction() 
    //  .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
    //  .commit(); 
    return true; 
    // http://www.vogella.com/tutorials/AndroidActionBar/article.html --- ACTIONBAR ACTION 
} 

// As AppointListFragment is added in this activity, so need to implement this listener here 
@Override 
public void onFragmentInteraction(Uri uri) { 
    System.out.println("Into onFragmentInteraction. URL - " + uri); 

    return; 
} 

WHERE我堅持: - 我不能夠做出來如何調用從下拉選擇時放下/套SettingFragment。並從Home圖標被按下時回到主頁AppointListFragment就可以了?

我在想什麼&問,請問&是否可行?我認爲應該是這樣的,因爲將自己設置爲活動ActionBar不是最佳解決方案。

請引導我與此,我已經搜索了很多教程,但無法找到這種情況或例子NavigationSelection &片段的任何地方。

回答

1

XML佈局中的碎片意味着是靜態碎片。我很可能以某種方式使它們工作,但對於動態生成/創建/刪除,你希望在Java中做所有事情。

說,首先你從你的XML中刪除<fragment,然後在您的活動創造:

@覆蓋 保護無效的onCreate(捆綁savedInstanceState){ super.onCreate(savedInstanceState); setContentView(...你的XML佈局...);

// fragments are auto-re-created on rotation, 
    // so only create/put in layout one if not rotating 
    if(savedInstanceState == null){ 

     // I'm assuming here your first fragment is position 0 
     gotoFragment(0); 
    } 

}

然後在每一個選擇只需要調用到新:

@Override 
public boolean onNavigationItemSelected(int position, long id) { 
    gotoFragment(position); 
    return true; 
} 

這裏是改變屏幕上的片段的一般方法。

private void gotoFragment(int position){ 
     Fragment f = getFragmentManager().findFragmentByTag("position_" + position); 
     if(f == null){ 
      switch(position){ 
       case values: 
        f = // create here new object of your fragment for each position 
      } 
     } 

     getFragmentManager().beginTransaction() 
       .replace(R.id.container, f, "position_" + position) 
       .commit(); 
} 
+0

謝謝@Budius。上面的代碼幫了我很多。但是我面臨着onCreate代碼的問題。由於我在ActionBar中啓用了Home,我還添加了要添加到主頁上的片段(position_0),所以我沒有在dropDown中添加Home。導航每次在'onCreate'中默認選擇第一個元素,所以在開始時我會看到我的第0個片段(position_0)和第1個片段(position_1)。我試過'actionBar.setSelectedNavigationItem(-1)',但它沒有區別。我不希望添加主頁,因爲主頁圖標點擊已經是他們的了。任何想法我怎麼能克服這個問題! – Tvd 2014-09-26 17:00:44

+0

選擇主頁後,在下拉選定項目上應該看不到任何內容。 – Tvd 2014-09-26 17:01:49