gpt4 book ai didi

android - 抽屉导航与 android 中的 fragment 重叠

转载 作者:行者123 更新时间:2023-11-29 01:45:57 25 4
gpt4 key购买 nike

我在我的主要 Activity 中创建了抽屉导航。当我点击抽屉导航项目时,我在我的主要 Activity 中启动 fragment 。如果再次点击抽屉导航图标,抽屉与 fragment 重叠。

我想在第二次打开抽屉导航时分离 fragment 。

Context mContext;
UIController mController;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mTitle;
private String[] mPlanetTitles = { "Photos", "Videos", "Settings" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "test";

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /*
* nav drawer icon to replace 'Up'
* caret
*/
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...

return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
selectItem(position);
mDrawerLayout.closeDrawer(mDrawerList);

}
}

public void selectItem(int position) {
switch (position) {
case 0:
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content,
AlbumListFragment.newInstance()).commit();
break;
}
}

最佳答案

看看remove(android.app.Fragment)popBackStackImmediate()

我假设你想要这样的东西:

...
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
//remove the fragment or pop it form the stack when you open the drawer?
getFragmentManager().popBackStackImmediate();
}
...

关于android - 抽屉导航与 android 中的 fragment 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com