Skip to content

Commit

Permalink
Fix Send Button hiding behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
rm3l committed Jul 8, 2017
1 parent c892988 commit 0a4e611
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.rm3l.maoni.ui;

import android.support.design.widget.AppBarLayout;

abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

public enum State {
EXPANDED,
COLLAPSED,
IDLE
}

private State mCurrentState = State.IDLE;

@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}

public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
32 changes: 11 additions & 21 deletions maoni/src/main/java/org/rm3l/maoni/ui/MaoniActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
Expand Down Expand Up @@ -264,27 +265,6 @@ protected void onCreate(Bundle savedInstanceState) {

final View fab = findViewById(R.id.maoni_fab);
if (fab != null) {
final ViewTreeObserver viewTreeObserver = fab.getViewTreeObserver();
if (viewTreeObserver == null) {
if (this.mMenu != null) {
final MenuItem item = this.mMenu.findItem(R.id.maoni_feedback_send);
if (item != null) {
item.setVisible(false);
}
}
} else {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (mMenu != null) {
final MenuItem item = mMenu.findItem(R.id.maoni_feedback_send);
if (item != null) {
item.setVisible(fab.getVisibility() != View.VISIBLE);
}
}
}
});
}
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -448,6 +428,16 @@ protected void onDestroy() {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.maoni_activity_menu, menu);
this.mMenu = menu;
final AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.maoni_app_bar);
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
final MenuItem item = mMenu.findItem(R.id.maoni_feedback_send);
if (item != null) {
item.setVisible(state != State.EXPANDED);
}
}
});
return super.onCreateOptionsMenu(menu);
}

Expand Down

0 comments on commit 0a4e611

Please sign in to comment.