Skip to content

Instantly share code, notes, and snippets.

@mzgreen
mzgreen / RecyclerHeaderViewHolder.java
Created February 15, 2015 11:17
HideOnScrollExample - RecyclerHeaderViewHolder class
public class RecyclerHeaderViewHolder extends RecyclerView.ViewHolder {
public RecyclerHeaderViewHolder(View itemView) {
super(itemView);
}
}
@mzgreen
mzgreen / HidingScrollListener.java
Created February 15, 2015 11:18
HideOnScrollExample - HidingScrollListener class
public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private static final int HIDE_THRESHOLD = 20;
private int scrolledDistance = 0;
private boolean controlsVisible = true;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
@mzgreen
mzgreen / HidingScrollListener.java
Created February 15, 2015 11:19
HideOnScrollExample - HidingScrollListener adding
if((controlsVisible && dy>0) || (!controlsVisible && dy<0)) {
scrolledDistance += dy;
}
@mzgreen
mzgreen / HidingScrollListener.java
Created February 15, 2015 11:20
HideOnScrollExample - HidingScrollListener logic
if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
onHide();
controlsVisible = false;
scrolledDistance = 0;
} else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
onShow();
controlsVisible = true;
scrolledDistance = 0;
}
@mzgreen
mzgreen / MainActivity.java
Created February 15, 2015 11:21
HideOnScrollExample - MainActivity recycler setup
private void initRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
recyclerView.setAdapter(recyclerAdapter);
//setting up our OnScrollListener
recyclerView.setOnScrollListener(new HidingScrollListener() {
@Override
public void onHide() {
hideViews();
@mzgreen
mzgreen / MainActivity.java
Last active August 29, 2015 14:15
HideOnScrollExample - MainActivity hide/show methods
private void hideViews() {
mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFabButton.getLayoutParams();
int fabBottomMargin = lp.bottomMargin;
mFabButton.animate().translationY(mFabButton.getHeight()+fabBottomMargin).setInterpolator(new AccelerateInterpolator(2)).start();
}
private void showViews() {
mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
@mzgreen
mzgreen / HidingScrollListener.java
Last active August 29, 2015 14:15
HideOnScrollExample - HidingScrollListener fixed onScroll
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
//show views if first item is first visible position and views are hidden
if (firstVisibleItem == 0) {
if(!controlsVisible) {
onShow();
controlsVisible = true;
@mzgreen
mzgreen / activity_part_two.xml
Created February 28, 2015 08:04
ActivityPartTwo layout file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false"/>
@mzgreen
mzgreen / ActivityPartTwo.java
Created February 28, 2015 08:07
ActivityPartTwo class
public class PartTwoActivity extends ActionBarActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeGreen);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_two);
@mzgreen
mzgreen / HidingScrollListener.java
Created February 28, 2015 08:10
HidingScrollListener class without snapping
public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private int mToolbarOffset = 0;
private int mToolbarHeight;
public HidingScrollListener(Context context) {
mToolbarHeight = Utils.getToolbarHeight(context);
}
@Override