diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2018-07-17 20:54:30 +0530 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-07-18 02:15:06 +0200 |
commit | fdfab18d45e6e58b37a5fafd2c37e3a78c2f3f12 (patch) | |
tree | 4585eeaae4219b1c897a8ee3dbaa935402a039a4 /app/src/main/java | |
parent | d43e77867c3a58e434307b4eb9469b1766ddc6c8 (diff) |
fab: make fab respond to recyclerview scroll events
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java | 20 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/widget/CustomRecyclerViewScrollListener.java | 38 |
2 files changed, 58 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index d14dc9e5..448a4834 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -26,6 +26,8 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.DecelerateInterpolator; import com.wireguard.android.Application; 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.model.Tunnel; import com.wireguard.android.util.ExceptionLoggers; +import com.wireguard.android.widget.CustomRecyclerViewScrollListener; import com.wireguard.config.Config; import java.io.BufferedReader; @@ -189,6 +192,23 @@ public class TunnelListFragment extends BaseFragment { } 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(); return binding.getRoot(); } diff --git a/app/src/main/java/com/wireguard/android/widget/CustomRecyclerViewScrollListener.java b/app/src/main/java/com/wireguard/android/widget/CustomRecyclerViewScrollListener.java new file mode 100644 index 00000000..2660179f --- /dev/null +++ b/app/src/main/java/com/wireguard/android/widget/CustomRecyclerViewScrollListener.java @@ -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(); +} |