简体   繁体   English

工具栏后退按钮功能

[英]toolbar back button functionality

I am having an issue with the toolbar and the back button. 我遇到了工具栏和后退按钮的问题。 Here is the setup I have: 这是我的设置:

在此输入图像描述

When I add a detail fragment, I animate the toolbar hamburger as outlined here. 当我添加一个细节片段时,我会按照此处所述为工具栏汉堡包设置动画 and this causes the hamburger to animate to an arrow. 这会使汉堡包成为箭头的动画。

Even in the comments section, a user mentions: 即使在评论部分,用户也会提到:

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. 只需将start = 0和end = 1设置为从汉堡包转到箭头,然后将start = 1和end = 0设置为箭头转换为汉堡包。 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. 此时,汉堡包最终被显示(因为抽屉的滑动),你必须纠正。

But I cannot figure out how to get the back arrow to function properly. 但我无法弄清楚如何使后箭头正常运行。 When I press the back arrow, the drawer opens and the detail fragment does not pop. 当我按下后箭头时,抽屉打开,细节片段不会弹出。 How should I go about implementing this? 我应该怎么做呢?

Questions 问题

  • How should I animate hamburger to back arrow when adding a detail fragment? 添加细节片段时,我应该如何为汉堡包添加动画效果? assuming the linked solution is not good enough. 假设链接的解决方案不够好。
  • How do I override the back arrow to perform only specific functions I wish? 如何覆盖后退箭头以仅执行我希望的特定功能? like animate to hamburger, pop back stack and NOT open the drawer. 喜欢给汉堡包制作动画,弹出后背堆叠而不打开抽屉。

After several hours of searching and playing around, I was able to build a solution that delivered on each requirement. 经过几个小时的搜索和游戏,我能够构建一个满足每个要求的解决方案。 Sources: 1 , 2 来源: 12

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

You can set listener for this button: 您可以为此按钮设置监听器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM