我在我的應用中實現了navigation slider
。在這個滑塊中,我有四個fragments
:Home,Profile,Search,calculator。不要擔心在選擇信息方法的命名約定,因爲我只專注於searchProgram截至目前,並沒有我所有的四個片段中實現 NavigationDrawer.class(活動):用導航滑塊點擊時替換操作欄向上按鈕
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
public void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomePage();
break;
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new SearchProgram();
default:
break;
}
if (fragment != null) {
Bundle bundle = new Bundle();
bundle.putInt(HomeFragment.EXTRA_ITEM_INDEX, position);
bundle.putString("username", userName);
bundle.putString("access_token", access_token);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
當我選擇搜索我被帶到一個視圖,其中有導航滑塊的action bar
就像我想要的一樣。當用戶從兩個旋鈕中選擇他們想要搜索的內容並點擊我自己的按鈕search時,我將啓動一個包含可擴展列表的master detail flow
。 下面是我從我的searchProgram類(片段)開始我的新活動:
protected void onPostExecute(ArrayList<SportProgram> programList) {
Bundle extras = new Bundle();
String userName = getArguments().getString("username");
String access_Token = getArguments().getString("accessToken");
System.out.println("Search Program Test Username: " + userName);
System.out.println("Search Program Test Access_Token: " + access_Token);
extras.putString("username", userName);
extras.putString("accessToken", access_Token);
extras.putParcelableArrayList("programs", programList);
Intent intent = new Intent(getActivity(), TrainingProgramListActivity.class);
intent.putExtras(extras);
startActivity(intent);
}
}
這個觀點有一個action bar
一回/向上按鈕,沒有導航滑塊。當我點擊這個按鈕時,我會回到之前的視圖,即搜索視圖,但是操作欄保持不變,並且仍然具有向上按鈕。有沒有人有任何想法如何更改操作欄,以包括導航滑塊像它以前做的?此外,當我在listActivity視圖中選擇返回/向上按鈕我更換當前片段如下:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list_program, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (item.getItemId()) {
//This is the back/up button
case android.R.id.home:
fragment = new SearchProgram();
break;
case R.id.action_home:
Intent homeIntent = new Intent(this, NavigationDrawer.class);
Bundle extras = this.getIntent().getExtras();
homeIntent.putExtras(extras);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
System.out.println("HIT HOMEINTENT");
return true;
case R.id.action_search:
fragment = new SearchProgram();
System.out.println("HIT SEARCH");
break;
default:
break;
}
if (fragment != null) {
Bundle bundle = this.getIntent().getExtras();//new Bundle();
// bundle.putString("username", userName);
// bundle.putString("access_token", access_token);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerE, fragment).commit();
// fragment.getActivity().supportInvalidateOptionsMenu();
}
return super.onOptionsItemSelected(item);
}
我已經試過無數種可能的解決方案,但繼續下跌短。我發現的所有例子都是從navigationDrawer活動導航到一個片段,然後是另一個片段,但對於我來說,我正在從NavigationDrawer導航到fragment-> class->片段。請有任何想法嗎?
回到第一個活動時,嘗試調用'finish()'而不是'new SearchProgram()'。即,關閉細節活動;你應該在後臺找到之前的活動,以及它的抽屜等等。 – natario
是的!謝謝你我一直堅持這一整個上午,並試圖我不知道有多少種不同的方式!再次感謝! – CH15