fab: make fab respond to recyclerview scroll events

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-07-17 20:54:30 +05:30 committed by Jason A. Donenfeld
parent d43e77867c
commit fdfab18d45
4 changed files with 63 additions and 1 deletions

View File

@ -26,6 +26,8 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
@ -35,6 +37,7 @@ import com.wireguard.android.databinding.TunnelListFragmentBinding;
import com.wireguard.android.databinding.TunnelListItemBinding; import com.wireguard.android.databinding.TunnelListItemBinding;
import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel;
import com.wireguard.android.util.ExceptionLoggers; import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.widget.CustomRecyclerViewScrollListener;
import com.wireguard.config.Config; import com.wireguard.config.Config;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -189,6 +192,23 @@ public class TunnelListFragment extends BaseFragment {
} }
return false; return false;
}); });
binding.tunnelList.setOnScrollListener(new CustomRecyclerViewScrollListener() {
@Override
public void show() {
binding.createMenu.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator(2))
.start();
}
@Override
public void hide() {
binding.createMenu.animate()
.translationY(binding.createMenu.getHeight() + getResources().getDimension(R.dimen.fab_margin))
.setInterpolator(new AccelerateInterpolator(2))
.start();
}
});
binding.executePendingBindings(); binding.executePendingBindings();
return binding.getRoot(); return binding.getRoot();
} }

View File

@ -0,0 +1,38 @@
/*
* Copyright © 2018 Harsh Shandilya <msfjarvis@gmail.com>
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.widget;
import android.support.v7.widget.RecyclerView;
public abstract class CustomRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
private int scrollDist;
private boolean isVisible = true;
private static final float FLING_THRESHOLD = 25;
@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isVisible && scrollDist > FLING_THRESHOLD) {
hide();
scrollDist = 0;
isVisible = false;
} else if (!isVisible && scrollDist < -FLING_THRESHOLD) {
show();
scrollDist = 0;
isVisible = true;
}
if (isVisible ? dy > 0 : dy < 0) {
scrollDist += dy;
}
}
public abstract void show();
public abstract void hide();
}

View File

@ -40,7 +40,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_margin="16dp" android:layout_margin="@dimen/fab_margin"
app:fab_labelStyle="@style/fab_label" app:fab_labelStyle="@style/fab_label"
android:clipChildren="false" android:clipChildren="false"
app:fab_labelsPosition="left" app:fab_labelsPosition="left"

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="fab_margin">16dp</dimen>
</resources>