gpt4 book ai didi

java - setDisplayHomeAsUpEnabled 关闭 ActionBarDrawerToggle

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:19 24 4
gpt4 key购买 nike

当我点击主页图标时,抽屉打开,然后立即将我带回之前的 Activity 。我相信 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.homeID_HOME 相同),它将返回 true并且 mDrawerToggle.isDrawerIndicatorEnabled()true(其默认值)。

关于java - setDisplayHomeAsUpEnabled 关闭 ActionBarDrawerToggle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170011/

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