t3rmian / devpot

A coder's blog
https://blog.termian.dev
1 stars 0 forks source link

posts/bottomappbar-onhide-listener/ #99

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

BottomAppBar onHide listener

https://blog.termian.dev/posts/bottomappbar-onhide-listener/

JayDoogson commented 3 years ago

First of all, thanks for posting such a helpful well-written article.

I'm having an issue though and hoped you could help. When I override with the onHideListener from the article, the FAB hides perfectly but calling performHide() or performShow() programmatically on the BottomAppBar no longer works. Any idea if there's a way to do this?

t3rmian commented 3 years ago

@JayDoogson Great question, this is because performHide() and performShow() use an inner state of Behavior, at least in version 1.3.0 of Google Material library:

package com.google.android.material.bottomappbar;
/**/
public class BottomAppBar extends Toolbar implements AttachedBehavior {
  /**/
  private Behavior behavior;

  /** Animates the {@link BottomAppBar} so it hides off the screen. */
  public void performHide() {
    getBehavior().slideDown(this);
  }

  /** Animates the {@link BottomAppBar} so it is shown on the screen. */
  public void performShow() {
    getBehavior().slideUp(this);
  }

  @NonNull
  @Override
  public Behavior getBehavior() {
    if (behavior == null) {
      behavior = new Behavior();
    }
    return behavior;
  }
}

Unfortunately, there is no setter for this property, but you could probably extend the BottomAppBar class replacing it in the layout XML, and override the getter with your own behavior. Otherwise, you could invoke the actions directly on the article's behavior as well: