我遇到了工具栏和后退按钮的问题。这是我的设置:
当我添加一个细节片段时,我按照概述here.设置工具栏汉堡包的动画,这会使汉堡包动画为箭头。
即使在评论部分,用户也会提到:
这完美无缺。只需设置start = 0和end = 1即可从汉堡包中取出 箭头,并开始= 1和结束= 0箭头到汉堡包。一件事 你必须跟踪当抽屉关闭的时候 箭头显示。此时,汉堡包最终被展示出来 (因为抽屉的滑动),你必须纠正。
但我无法弄清楚如何让后箭头正常运作。当我按下后箭头时,抽屉打开,细节片段不会弹出。我该如何实施呢?
答案 0 :(得分:3)
经过几个小时的搜索和游戏,我能够构建一个满足每个要求的解决方案。来源:1,2
detailFragmentActive = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(mToolbar);
...
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(detailFragmentActive) {
onBackPressed();
//if(displayBackAgain)
//return; //return after so you don't call syncState();
}else if (mDrawerLayou.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
mDrawerLayout.openDrawer(GravityCompat.START);
mDrawerToggle.syncState();
}
});
}
private void animateHamburger(boolean isArrow){
int start = 0, end = 1;
if(isArrow){
detailFragmentActive = false;
start = 1; end = 0;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
detailFragmentActive = true;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mDrawerToggle.onDrawerSlide(mDrawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}
@Override
public void onBackPressed() {
super.onBackPressed();
animateHamburger(true);
}
public void onFragmentChange(){
...
animateHamburger(false);
}
答案 1 :(得分:1)
您可以为此按钮设置监听器:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flagDeterminingAction) {
drawerLayout.openDrawer(drawerListView);
} else {
onBackPressed();
//or popbackstack or whatever you are using to going back in navigation
}
}