java-setDisplayHomeAsUpEnabled关闭ActionBarDrawerToggle
作者:互联网
当我单击主页图标时,抽屉将打开,然后立即将我带回到上一个活动.我相信setDisplayHomeAsUpEnabled(true)会干扰操作栏抽屉保持打开状态.
代码中是否有任何原因可能导致这种情况?
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
public class GenericActivity extends FragmentActivity implements SearchView.OnQueryTextListener {
/** Slide out drawer navigation **/
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_draft);
// Set up drawer navigation.
mTitle = mDrawerTitle = getTitle();
mTitles = getResources().getStringArray(R.array.in_draft_menu_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// Set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.in_draft, menu);
// Inflate the menu; this adds items to the action bar if it is present.
MenuItem statusItem = menu.findItem(R.id.draftStatusMenuItem);
mChatItem = menu.findItem(R.id.chatMenuItem);
MenuItem settingsItem = menu.findItem(R.id.settingsMenuItem);
final MenuItem refreshItem = menu.findItem(R.id.refreshMenuItem);
final MenuItem searchItem = menu.findItem(R.id.action_search);
if (mSearchFieldToggleState == 1) {
searchItem.setVisible(true);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnQueryTextListener(this);
// Close the search when mSearchView is not focused. Also, show/hide the refresh menu button depending on the state.
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean queryTextFocused) {
if(!queryTextFocused) {
searchItem.collapseActionView();
mSearchView.setQuery("", true);
refreshItem.setVisible(true);
} else {
refreshItem.setVisible(false);
}
}
});
} else {
searchItem.setVisible(false);
}
if (refreshItem == null) {
return true;
}
// Set this menu item to refresh the league.
refreshItem.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return draftRefreshMenuItem(item);
}
});
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.refreshMenuItem).setVisible(!drawerOpen);
menu.findItem(R.id.action_search).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
mSearchFieldToggleState = 0;
invalidateOptionsMenu();
// When the given dropdown item is selected, show its contents in the
// container view.
if (position == 0){
mSearchFieldToggleState = 1;
invalidateOptionsMenu();
Fragment fragment = new DraftSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
} else if (position == 1) {
Fragment fragment = new MyTeamSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
} else if (position == 2) {
Fragment fragment = new RostersSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
} else if (position == 3) {
Fragment fragment = new ResultsSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Handle the query text changing. Submit the query to the filter.
* @param query
* @return
*/
public boolean onQueryTextChange(String query) {
if (mDraftPlayerAdapter != null && mDraftViewPager != null && mDraftPlayerAdapter.getFragmentReferenceMap() != null
&& mDraftPlayerAdapter.getFragmentReferenceMap().get(mDraftViewPager.getCurrentItem()) != null) {
mDraftPlayerAdapter.getFragmentReferenceMap().get(mDraftViewPager.getCurrentItem()).getFilter().filter(query);
}
return false;
}
/**
* Handle the query text being submitted - with a return key. Same process as onQueryTextChange.
* @param query
* @return
*/
public boolean onQueryTextSubmit(String query) {
onQueryTextChange(query);
return false;
}
}
解决方法:
在早期版本的支持库中,mDrawerToggle.onOptionsItemSelected(item)始终返回false-确保您使用的是最新版本的Support Library(或至少是18或更高版本).
这是支持库的r18中的最新代码:
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == ID_HOME && mDrawerIndicatorEnabled) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return true;
}
return false;
}
如您所见,如果传入的项目是主页按钮(android.R.id.home与ID_HOME相同)并且mDrawerToggle.isDrawerIndicatorEnabled()为true(默认值),它将返回true.
标签:java,android,drawerlayout 来源: https://codeday.me/bug/20191030/1967198.html