gpt4 book ai didi

android - 工具栏后退按钮功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:29 25 4
gpt4 key购买 nike

我在使用工具栏和后退按钮时遇到问题。这是我的设置:

enter image description here

当我添加细节 fragment 时,我按照概述为工具栏汉堡包制作动画 here.这会导致汉堡包变成一个箭头。

即使在评论部分,用户也会提到:

This works perfectly. Just set start=0 and end=1 to go from hamburger to arrow, and start=1 and end=0 for arrow to hamburger. One thing you'll have to keep track of is when the drawer is closed when the arrow is shown. At this point, the hamburger ends up being shown (because of the drawer's slide), which you'll have to correct.

但我不知道如何让后退箭头正常工作。当我按下后退箭头时,抽屉打开并且细节 fragment 不会弹出。我应该如何实现它?

问题

  • 在添加细节 fragment 时,我应该如何为汉堡包制作后退箭头动画?假设 linked解决方案不够好。
  • 如何覆盖后退箭头以仅执行我希望的特定功能?就像汉堡包动画一样,弹出堆栈并且打开抽屉。

最佳答案

经过几个小时的搜索和尝试,我能够构建满足每个需求的解决方案。资料来源: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);
}

关于android - 工具栏后退按钮功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171483/

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