无论我尝试做什么,UpButton 都不起作用
UpButton doesn't work whatever I try to do
在我的应用程序 (minSdkVersion
15) 中,我有一个 Toolbar
而不是 ActionBar
和 NavigationDrawer
在片段之间切换。有些片段有 TabBar
,里面有 child 个片段。这些 child 个片段是 ListView
个,它们的 onItemClickListener
个触发器 DetailFragment
。我调用 setDisplayHomeAsUpEnabled()
并出现 DetailFragment
的向上箭头,但我无法对其执行任何操作,甚至无法祝酒。我已经尝试 this 并在 onOptionsItemSelected()
中处理 switch (item.getItemId())
但这两种解决方案都不适合我。我遗漏了一些东西,但无法识别是什么。
这是我的代码:
public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar(); //...
...
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
actionBarDrawerToggle.setDrawerIndicatorEnabled(!canback);
}
}
@Override
public boolean onNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
我的整个onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show();
//called when the up affordance/carat in actionbar is pressed
onBackPressed();
break;
case R.id.action_search:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
我不知道其他代码是否重要:
// Initializing Drawer Layout and ActionBarToggle
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.openDrawer, R.string.closeDrawer){//...
//Setting the actionbarToggle to drawer layout
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
在我的清单中我没有 parent activity.
我找到了 this,这似乎是正确的答案,但我不明白如何实现它,也没有资格问作者。
我怎样才能让向上按钮正常工作?
两个新问题:当我设置
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
}
...然后从 child 片段我按硬件 "back" 按钮 I return 到前一个片段,汉堡包图标出现。但是向上按钮仍然没有来自 child 片段的响应。我相信这是因为 ActionBarDrawerToggle
不再管理其行为。但是谁来管理它呢?如果我这样设置此方法:
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
}
...然后 ActionBarDrawerToggle
处理向上按钮的点击并像汉堡包图标一样打开抽屉。但是当我按下硬件后退按钮时,向上按钮(箭头)消失并且汉堡包图标没有出现。
所以现在我看到了两种解决这个问题的方法。首先,我可以弄清楚谁在管理向上按钮
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
和
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
否则,当向上按钮箭头而不是汉堡包图标可用时,我如何覆盖 ActionBarDrawerToggle
行为以执行另一个操作?以及如何在我按下硬件后退按钮时使汉堡包图标出现?
您似乎没有处理 onOptionsItemSelected()
中的 ActionBarDrawerToggle
。还有 return true
用于消耗选择。尝试以下操作:
@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 (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show();
//called when the up affordance/carat in actionbar is pressed
onBackPressed();
return true;
case R.id.action_search:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
答案如我所料很简单,但我花了一天多的时间才弄清楚。我逐步实现了抽屉,发现了每种方法的文档。 运行 应用程序通知任何更改。我发现当 setDisplayHomeAsUpEnabled(true) 时,它只会更改后退箭头图标上的抽屉汉堡包图标,但是当您单击后退箭头时,它会执行相同的操作(显示隐藏抽屉)。
如果您想为后退箭头实现另一种行为,您也应该 setDrawerIndicatorEnabled(false) 然后 setToolbarNavigationClickListener()。所以
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false); // order matters
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
和
actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ToolbarNavigationClickListener", Toast.LENGTH_SHORT).show();
}
});
共同努力实现自定义 UpButton 行为。希望对大家有所帮助。
在我的应用程序 (minSdkVersion
15) 中,我有一个 Toolbar
而不是 ActionBar
和 NavigationDrawer
在片段之间切换。有些片段有 TabBar
,里面有 child 个片段。这些 child 个片段是 ListView
个,它们的 onItemClickListener
个触发器 DetailFragment
。我调用 setDisplayHomeAsUpEnabled()
并出现 DetailFragment
的向上箭头,但我无法对其执行任何操作,甚至无法祝酒。我已经尝试 this 并在 onOptionsItemSelected()
中处理 switch (item.getItemId())
但这两种解决方案都不适合我。我遗漏了一些东西,但无法识别是什么。
这是我的代码:
public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar(); //...
...
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
actionBarDrawerToggle.setDrawerIndicatorEnabled(!canback);
}
}
@Override
public boolean onNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
我的整个onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show();
//called when the up affordance/carat in actionbar is pressed
onBackPressed();
break;
case R.id.action_search:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
我不知道其他代码是否重要:
// Initializing Drawer Layout and ActionBarToggle
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.openDrawer, R.string.closeDrawer){//...
//Setting the actionbarToggle to drawer layout
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
在我的清单中我没有 parent activity.
我找到了 this,这似乎是正确的答案,但我不明白如何实现它,也没有资格问作者。
我怎样才能让向上按钮正常工作?
两个新问题:当我设置
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
}
...然后从 child 片段我按硬件 "back" 按钮 I return 到前一个片段,汉堡包图标出现。但是向上按钮仍然没有来自 child 片段的响应。我相信这是因为 ActionBarDrawerToggle
不再管理其行为。但是谁来管理它呢?如果我这样设置此方法:
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
}
...然后 ActionBarDrawerToggle
处理向上按钮的点击并像汉堡包图标一样打开抽屉。但是当我按下硬件后退按钮时,向上按钮(箭头)消失并且汉堡包图标没有出现。
所以现在我看到了两种解决这个问题的方法。首先,我可以弄清楚谁在管理向上按钮
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
和
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
否则,当向上按钮箭头而不是汉堡包图标可用时,我如何覆盖 ActionBarDrawerToggle
行为以执行另一个操作?以及如何在我按下硬件后退按钮时使汉堡包图标出现?
您似乎没有处理 onOptionsItemSelected()
中的 ActionBarDrawerToggle
。还有 return true
用于消耗选择。尝试以下操作:
@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 (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show();
//called when the up affordance/carat in actionbar is pressed
onBackPressed();
return true;
case R.id.action_search:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
答案如我所料很简单,但我花了一天多的时间才弄清楚。我逐步实现了抽屉,发现了每种方法的文档。 运行 应用程序通知任何更改。我发现当 setDisplayHomeAsUpEnabled(true) 时,它只会更改后退箭头图标上的抽屉汉堡包图标,但是当您单击后退箭头时,它会执行相同的操作(显示隐藏抽屉)。
如果您想为后退箭头实现另一种行为,您也应该 setDrawerIndicatorEnabled(false) 然后 setToolbarNavigationClickListener()。所以
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false); // order matters
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
和
actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ToolbarNavigationClickListener", Toast.LENGTH_SHORT).show();
}
});
共同努力实现自定义 UpButton 行为。希望对大家有所帮助。