From de951ee5350552ebbb88db05457020f62037cdaa Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 17 Jul 2018 20:54:30 +0530 Subject: fab: make fab respond to recyclerview scroll events Signed-off-by: Harsh Shandilya --- .../android/fragment/TunnelListFragment.java | 20 ++++++++++++ .../widget/CustomRecyclerViewScrollListener.java | 38 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 app/src/main/java/com/wireguard/android/widget/CustomRecyclerViewScrollListener.java (limited to 'app/src/main/java/com') 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 + * Copyright © 2018 Jason A. Donenfeld . 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(); +} -- cgit v1.2.3