我有一个活动,其中包含一个包含片段的视图寻呼机。我还有一个带有碎片的导航抽屉。每当我启动我的应用程序时,在主要活动中,我将第一个片段添加到片段管理器中,因此每当应用程序启动时它都会显示。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = new FragmentHome();
mainToolbar = (Toolbar) findViewById(R.id.mainActivityToolBar);
setSupportActionBar(mainToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if(savedInstanceState == null){
home = new FragmentHome();
FragmentManager manager = getSupportFragmentManager();
// changed the back stack here..
String Tag = "start";
manager.beginTransaction().add(R.id.layout_container, home, Tag).commit();
getSupportActionBar().setTitle("Home");
}
FragmentNavigationDrawer fragmentNavigationDrawer = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_contain_drawer);
fragmentNavigationDrawer.setUpDrawer(R.id.fragment_contain_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mainToolbar);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "fragment", home);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null){
home = getSupportFragmentManager().getFragment(savedInstanceState, "fragment");
}
}
@Override
public void changeToolBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
在导航抽屉适配器中,我有一个获取片段的方法,并在需要时显示或添加它们。我无法解决如何解决的主要问题是我正在尝试保持第一个片段的状态(其中包含带有自定义对象数组列表的回收器视图),同时切换到抽屉中的其他片段并显示其内容< / p>
public void onNavigationDrawerItemSelected(int position){
// Add the fragments only once
if (getFragmentManager().findFragmentByTag(navigationDrawerFragmentsTags[position]) == null)
{
fragmentTransaction.add(R.id.layout_container,navigationDrawerFragments[position],navigationDrawerFragmentsTags[position]);
}
// Hiding & Showing fragments
for(int i=0;i<navigationDrawerFragments.length;i++)
{
if(i == position)
{
fragmentTransaction.show(navigationDrawerFragments[i]);
}
else
{
// Check if the fragment is added and then hide it
if (getFragmentManager().findFragmentByTag(navigationDrawerFragmentsTags[i]) != null) {
fragmentTransaction.hide(navigationDrawerFragments[i]);
}
}
}
fragmentTransaction.commit();
}
然而,添加的第一个片段总是存在于其他层之下,我无法隐藏它。我想保持状态,但是当我要去另一个片段时我也要隐藏,当用户回到这个片段时我需要保持其内容显示列表。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
itemList = (ListView) v.findViewById(R.id.drawer_list_items);
adapter = new NavigationDrawerAdapter(getContext(), R.layout.drawer_item_views, addItemsToList());
itemList.setAdapter(adapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
onNavigationDrawerItemSelected(position);
myDrawerLayout.closeDrawers();
}
});
return v;
}
我尝试使用界面,我试图查看谷歌,但我找不到答案。
//
答案 0 :(得分:0)
保持状态,将其添加到backstack
而不是添加新片段,只需替换您的片段,即删除旧片段,然后添加新片段。即fragmentTransaction.replace()而不是fragmentTransaction.add()