From 9e6602f114962dfd4450f680475b0fad3b5e9988 Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 12 Nov 2017 15:41:28 +0100 Subject: [PATCH] Hiding FABs on scroll. --- CHANGELOG.md | 4 +++ .../bou/readerforselfoss/ReaderActivity.kt | 8 +++++ .../bou/readerforselfoss/SourcesActivity.kt | 10 +++++- .../utils/ScrollAwareFABBehavior.kt | 33 +++++++++++++++++++ app/src/main/res/layout/activity_sources.xml | 3 +- 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/ScrollAwareFABBehavior.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b816f2..bbefe3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +**master** + +- Hiding FABs on scroll. + **1.5.4.10** - Displaying a loader when "reading more" in the article viewer. diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 5b851ca..0644755 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -124,6 +124,14 @@ class ReaderActivity : AppCompatActivity() { image.visibility = View.GONE } } + + nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, oldScrollY -> + if (scrollY > oldScrollY) { + fab.hide() + } else { + fab.show() + } + }) } private fun getContentFromMercury(customTabsIntent: CustomTabsIntent, prefs: SharedPreferences) { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt index 277b707..aa0ae6d 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt @@ -8,6 +8,7 @@ import android.support.v7.app.AppCompatActivity import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.support.v7.widget.Toolbar +import android.view.View import android.widget.Toast import apps.amine.bou.readerforselfoss.adapters.SourcesListAdapter import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi @@ -20,6 +21,8 @@ import retrofit2.Response class SourcesActivity : AppCompatActivity() { + private lateinit var mRecyclerView: RecyclerView + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Scoop.getInstance().apply(this) @@ -30,10 +33,15 @@ class SourcesActivity : AppCompatActivity() { supportActionBar?.setDisplayShowHomeEnabled(true) } + override fun onStop() { + super.onStop() + mRecyclerView.clearOnScrollListeners() + } + override fun onResume() { super.onResume() val mFab: FloatingActionButton = findViewById(R.id.fab) - val mRecyclerView: RecyclerView = findViewById(R.id.activity_sources) + mRecyclerView = findViewById(R.id.activity_sources) val mLayoutManager = LinearLayoutManager(this) val prefs = PreferenceManager.getDefaultSharedPreferences(this) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ScrollAwareFABBehavior.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ScrollAwareFABBehavior.kt new file mode 100644 index 0000000..50f5c9d --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ScrollAwareFABBehavior.kt @@ -0,0 +1,33 @@ +package apps.amine.bou.readerforselfoss.utils + +import android.content.Context +import android.support.design.widget.CoordinatorLayout +import android.support.design.widget.FloatingActionButton +import android.util.AttributeSet +import android.view.View + +class ScrollAwareFABBehavior(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior() { + + override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, + directTargetChild: View, target: View, nestedScrollAxes: Int): Boolean { + return true + } + + override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, + child: FloatingActionButton, + target: View, dxConsumed: Int, dyConsumed: Int, + dxUnconsumed: Int, dyUnconsumed: Int) { + super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed) + if (dyConsumed > 0 && child.visibility == View.VISIBLE) { + child.hide(object : FloatingActionButton.OnVisibilityChangedListener() { + override fun onHidden(fab: FloatingActionButton?) { + super.onHidden(fab) + fab!!.visibility = View.INVISIBLE + } + }) + + } else if (dyConsumed < 0 && child.visibility != View.VISIBLE) { + child.show() + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sources.xml b/app/src/main/res/layout/activity_sources.xml index a6f1b8c..cf15f7b 100644 --- a/app/src/main/res/layout/activity_sources.xml +++ b/app/src/main/res/layout/activity_sources.xml @@ -43,5 +43,6 @@ android:layout_alignParentEnd="true" android:layout_marginBottom="16dp" android:layout_marginEnd="16dp" - android:layout_marginRight="16dp" /> + android:layout_marginRight="16dp" + app:layout_behavior="apps.amine.bou.readerforselfoss.utils.ScrollAwareFABBehavior" />