在我的應用程序中,我在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 &片段的任何地方。
謝謝@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
選擇主頁後,在下拉選定項目上應該看不到任何內容。 – Tvd 2014-09-26 17:01:49