Last active
May 4, 2016 16:11
-
-
Save ffgiraldez/d9ae0d97514a5779f69a to your computer and use it in GitHub Desktop.
Disable toolbar scroll flag when content it's not enough to fill the screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ToolbarActivity extends AppCompatActivity { | |
// Set the flags that fit your needs | |
private static final int ENABLED_SCROLL_BEHAVIOR = AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL; | |
private static final int DISABLED_SCROLL_BEHAVIOR = 0; | |
private static final int SCROLL_DOWN = 1; | |
//Injected via ButterKnife (http://jakewharton.github.io/butterknife) | |
@InjectView(R.id.toolbar) | |
Toolbar toolbar; | |
@InjectView(R.id.recyclerview) | |
RecyclerView recyclerView; | |
private final View.OnLayoutChangeListener recyclerViewChangeListener = new View.OnLayoutChangeListener() { | |
@Override | |
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { | |
updateToolbarScrollBehavior(); | |
} | |
}; | |
@Override | |
public void onResume() { | |
super.onResume(); | |
recyclerView.addOnLayoutChangeListener(recyclerViewChangeListener); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
recyclerView.removeOnLayoutChangeListener(recyclerViewChangeListener); | |
} | |
private void updateToolbarScrollBehavior() { | |
applyScrollBehavior(DISABLED_SCROLL_BEHAVIOR); | |
if (recyclerView.canScrollVertically(SCROLL_DOWN)) { | |
applyScrollBehavior(ENABLED_SCROLL_BEHAVIOR); | |
} | |
} | |
private void applyScrollBehavior(int scrollFlags) { | |
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); | |
layoutParams.setScrollFlags(scrollFlags); | |
toolbar.setLayoutParams(layoutParams); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
canScrollVertically
usegetWith()
andgetHeight()
internally so use LayoutChangeListener to ensure that those values are updated before callupdateToolbarScrollBehavior