Skip to content

Commit

Permalink
Auto hide FAB on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
outadoc committed Nov 12, 2016
1 parent 167089f commit 3e17707
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 70 deletions.
20 changes: 19 additions & 1 deletion twistoast/src/main/java/fr/outadev/twistoast/FragmentRealtime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.os.Bundle
import android.os.Handler
import android.support.design.widget.Snackbar
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import com.google.android.gms.ads.AdListener
Expand All @@ -38,7 +39,6 @@ import fr.outadev.twistoast.background.NextStopAlarmReceiver
import fr.outadev.twistoast.uiutils.DividerItemDecoration
import kotlinx.android.synthetic.main.fragment_realtime.*
import kotlinx.android.synthetic.main.view_no_content.*
import kotlinx.android.synthetic.main.view_realtime_list.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

Expand Down Expand Up @@ -120,6 +120,24 @@ class FragmentRealtime : Fragment(), IStopsListContainer {
}
}

stopsRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

if (dy > 0) {
// Scroll Down
if (floatingActionButton.isShown) {
floatingActionButton.hide()
}
} else if (dy < 0) {
// Scroll Up
if (!floatingActionButton.isShown) {
floatingActionButton.show()
}
}
}
})

registerForContextMenu(stopsRecyclerView)

setupAdvertisement()
Expand Down
96 changes: 55 additions & 41 deletions twistoast/src/main/res/layout/fragment_realtime.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Twistoast - fragment_realtime.xml
~ Copyright (C) 2013-2016 Baptiste Candellier
~
Expand All @@ -17,53 +16,68 @@
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<LinearLayout
android:id="@+id/main_lin_layout"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_lin_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:fitsSystemWindows="true"
android:orientation="vertical">

<include layout="@layout/view_traffic_alert" />
<include layout="@layout/view_traffic_alert" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/view_realtime_list" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/stopsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:clipToPadding="false"
android:divider="@null"
android:listSelector="@android:color/transparent" />

<include layout="@layout/view_no_content" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:contentDescription="@string/fab_add_stop_description"
android:src="@drawable/ic_action_new"
app:layout_anchor="@id/stopsRecyclerView"
app:layout_anchorGravity="bottom|right|end" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_margin="20dp"
android:src="@drawable/ic_action_new"
android:contentDescription="@string/fab_add_stop_description" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>
<include layout="@layout/view_no_content" />

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/admob_adunitid" />
</RelativeLayout>

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/admob_adunitid" />

</LinearLayout>
28 changes: 0 additions & 28 deletions twistoast/src/main/res/layout/view_realtime_list.xml

This file was deleted.

0 comments on commit 3e17707

Please sign in to comment.