无论我尝试做什么,UpButton都无法正常工作

时间:2015-06-14 03:53:52

标签: android android-fragments up-button

在我的应用中(minSdkVersion 15)我在片段之间切换Toolbar而不是ActionBarNavigationDrawer。有些片段里面有TabBar个子片段。这些子片段为ListView个,onItemClickListener触发DetailFragment。我拨打setDisplayHomeAsUpEnabled()并显示DetailFragment的向上箭头,但我不能将其视为执行任何操作,甚至是敬酒。我已尝试this并在switch (item.getItemId())中处理onOptionsItemSelected(),但这两种解决方案都不适合我。我错过了什么,却无法辨认出什么。

这是我的代码:

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();

在我的清单中,我没有父母活动。

我找到了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);
        }
    }
}

...从子片段我按下硬件“后退”按钮我返回上一个片段,然后出现汉堡包图标。但是向上按钮仍然没有来自子片段的响应。我相信这是因为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行为以执行其他操作?当我按下硬件后退按钮时,如何使汉堡包图标出现?

2 个答案:

答案 0 :(得分:3)

答案很简单,但我花了一天多的时间才弄明白。我逐步实现抽屉发现每种方法的文档。跑应用程序注意任何更改。我发现当 setDisplayHomeAsUpEnabled(true)时,它只会改变后箭头图标上的抽屉汉堡包图标,但当你点击后箭头时它会执行相同的动作(显示 - 隐藏抽屉)。

如果要为后退箭头实现其他行为,则应 setDrawerIndicatorEnabled(false),然后 setToolbarNavigationClickListener()。所以

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Title,Summary,Content")] Post post) {
    if (ModelState.IsValid) {
        var newsPost = db.Posts.Find(post.Id);
        if (newsPost == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
        newsPost.Title = post.Title;
        newsPost.Summary = post.Summary;
        newsPost.Content = post.Content;
        db.Entry(newsPost).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(post);
}

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);
    }
}

协同工作以实现自定义UpButton行为。希望它会帮助别人。

答案 1 :(得分:0)

好像你没有处理ActionBarDrawerToggle里面的onOptionsItemSelected()。同时返回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);
}