5

在我的应用程序(minSdkVersion15)中,我有一个片段之间的Toolbar代替ActionBarNavigationDrawer切换。有些片段TabBar里面有子片段。这些子片段是ListViews 和它们的onItemClickListener触发器DetailFragment。我打电话setDisplayHomeAsUpEnabled()并出现一个向上箭头,DetailFragment但我不能让它执行任何操作,甚至显示敬酒。我已经尝试过处理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();

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

我发现了这一点,这似乎是正确的答案,但我不明白如何实现它,也没有声望去问作者。

我可以尝试什么让向上按钮工作?

两个新问题:当我设置

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否则,当向上按钮箭头可用而不是汉堡包图标时,如何覆盖该行为以执行另一个操作?当我按下硬件返回按钮时,如何使汉堡图标出现?

4

2 回答 2

3

正如我所料,答案很简单,但我花了一天多的时间才弄清楚这一点。我逐步实现了抽屉,为每种方法发现了文档。运行应用程序以注意任何更改。我发现当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 行为。希望它会帮助某人。

于 2015-06-16T10:40:48.130 回答
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);
}
于 2015-06-15T06:22:57.620 回答