关闭工具栏背景设计

时间:2019-01-18 16:22:06

标签: android material-design onclicklistener android-toolbar

在按下工具栏之前,我已创建此视图:

before pressing toolbar

当我按下工具栏图标时,当我按下工具栏时,它会显示如下:

when I pressed on toolbar

因此,在第二张图片中,我想通过按任意红色按钮(即“ btn1”或“ btn2”)关闭工具栏,使其处于先前状态。我该怎么办?

我的MainActivity是:

public class Attendence extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attendence);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbars);
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new IconNavigationClickListener(this,findViewById(R.id.nested_view_scroll)
        ,new BounceInterpolator(),this.getResources().getDrawable(R.drawable.icon),
                this.getResources().getDrawable(R.drawable.arrow_down)));


    }
}

我的IconNavigationClickListener类是

public class IconNavigationClickListener implements View.OnClickListener {

    private AnimatorSet animatorSet = new AnimatorSet();
    private Context context;
    private View sheet;
    private Interpolator interpolator;
    private int height;
    private boolean backdropshow = false;
    private Drawable openIcon;
    private Drawable closeIcon;

    public IconNavigationClickListener(Context context, View sheet) {
        this(context,sheet,null);
    }
    public IconNavigationClickListener(Context context, View sheet, @Nullable Interpolator interpolator){
        this(context,sheet,interpolator,null,null);
    }
    public IconNavigationClickListener(Context context, View sheet, @Nullable Interpolator interpolator,@Nullable Drawable openIcon, @Nullable Drawable closeIcon)
    {
        this.context = context;
        this.sheet = sheet;
        this.interpolator = interpolator;
        this.openIcon = openIcon;
        this.closeIcon = closeIcon;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        height = displayMetrics.heightPixels;
        height = (int) (height/1.5);
    }


    @Override
    public void onClick(View v) {
       onclickListener(v);
    }

    public void onclickListener(View v)
    {
        backdropshow =! backdropshow;

        animatorSet.removeAllListeners();
        animatorSet.end();
        animatorSet.cancel();
        updateIcon(v);

        final int translateY = height - context.getResources().getDimensionPixelSize(R.dimen.shr_product_grid_reveal_height);

        ObjectAnimator animator = ObjectAnimator.ofFloat(sheet,"TranslationY",backdropshow ? translateY : 0);
        animator.setDuration(900);
        if(interpolator != null)
        {
            animator.setInterpolator(interpolator);
        }
        animatorSet.play(animator);
        animator.start();
    }

    private void updateIcon(View v){
        if(openIcon != null && closeIcon !=null)
        {
            if(!(v instanceof ImageView)){
                throw new IllegalArgumentException("UpdateIcon() must be call on imageView");
            }
            if(backdropshow){
                ((ImageView)v).setImageDrawable(closeIcon);
            }else{
                ((ImageView)v).setImageDrawable(openIcon);
            }
        }
    }
}

我使用“上课”来显示动画

0 个答案:

没有答案