单击时将操作栏向上按钮替换为导航滑块
replace action bar up button with navigation slider when it is clicked
我在我的应用程序中实现了 navigation slider
。在这个滑块中,我有四个 fragments
:主页、配置文件、搜索、计算器。不要担心 selectItem 方法中的命名约定,因为我现在只关注 searchProgram 并且没有实现我的所有四个片段
NavigationDrawer.class (Activity):
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);
}
当我 select 搜索时,我被带到一个视图,其中有一个 action bar
和我想要的导航滑块。当用户 select 从两个微调器中搜索他们想要搜索的内容并单击我自己的名为搜索的按钮时,我将启动一个包含可扩展列表的 master detail flow
。
下面是我从我的 searchProgram class(Fragment):
开始我的新 activity 的地方
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);
}
}
此视图有一个带有 back/up 按钮且没有导航滑块的 action bar
。当我单击此按钮时,我将返回到上一个视图,即搜索视图,但是操作栏保持不变并且仍然有向上按钮。有没有人知道如何像以前一样更改操作栏以包含导航滑块?同样,当我 select 列表中的 back/up 按钮 Activity 视图时,我将按如下方式替换当前片段:
@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);
}
我已经尝试了很多可能的解决方案,但总是达不到要求。我找到的所有示例都从导航抽屉 activity 导航到一个片段,然后导航到另一个片段,但对我来说,我是从导航抽屉导航到一个片段->class->片段。有什么想法吗?
您正在将一个细节 activity 堆叠在放置抽屉的主要部分之上。要返回原来的 activity,只需在您的 onOptionsItemSelected()
中使用 finish()
关闭最后一个。
我在我的应用程序中实现了 navigation slider
。在这个滑块中,我有四个 fragments
:主页、配置文件、搜索、计算器。不要担心 selectItem 方法中的命名约定,因为我现在只关注 searchProgram 并且没有实现我的所有四个片段
NavigationDrawer.class (Activity):
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);
}
当我 select 搜索时,我被带到一个视图,其中有一个 action bar
和我想要的导航滑块。当用户 select 从两个微调器中搜索他们想要搜索的内容并单击我自己的名为搜索的按钮时,我将启动一个包含可扩展列表的 master detail flow
。
下面是我从我的 searchProgram class(Fragment):
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);
}
}
此视图有一个带有 back/up 按钮且没有导航滑块的 action bar
。当我单击此按钮时,我将返回到上一个视图,即搜索视图,但是操作栏保持不变并且仍然有向上按钮。有没有人知道如何像以前一样更改操作栏以包含导航滑块?同样,当我 select 列表中的 back/up 按钮 Activity 视图时,我将按如下方式替换当前片段:
@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);
}
我已经尝试了很多可能的解决方案,但总是达不到要求。我找到的所有示例都从导航抽屉 activity 导航到一个片段,然后导航到另一个片段,但对我来说,我是从导航抽屉导航到一个片段->class->片段。有什么想法吗?
您正在将一个细节 activity 堆叠在放置抽屉的主要部分之上。要返回原来的 activity,只需在您的 onOptionsItemSelected()
中使用 finish()
关闭最后一个。