XML : Android Set FAB Behavior for ListView

I'm currently developing an application which have a ListView and a FAB. Now what i want to do is hide the FAB on the beginning and then show it after my ListView is scroll down. I have my content_main.xml where the ListView is coded, and activity_main.xml where my FAB is.

activity_main.xml

  <?xml version="1.0" encoding="utf-8"?>  <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:fitsSystemWindows="true"      tools:context=".MainActivity">        <android.support.design.widget.AppBarLayout          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:theme="@style/AppTheme.AppBarOverlay">            <android.support.v7.widget.Toolbar              android:id="@+id/toolbar"              android:layout_width="match_parent"              android:layout_height="?attr/actionBarSize"              android:background="?attr/colorPrimary"              app:popupTheme="@style/AppTheme.PopupOverlay" />        </android.support.design.widget.AppBarLayout>        <include layout="@layout/content_main" />        <android.support.design.widget.FloatingActionButton          android:id="@+id/fab"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="bottom|end"          android:layout_margin="@dimen/fab_margin"          android:src="@drawable/ic_up" />    </android.support.design.widget.CoordinatorLayout>    

content_main.xml

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      app:layout_behavior="@string/appbar_scrolling_view_behavior"      tools:context=".MainActivity"      tools:showIn="@layout/activity_main">            <ListView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:id="@+id/words_list_view"              android:divider="#00000000"              android:listSelector="@drawable/list_selector"              android:layout_weight="1" />  </RelativeLayout>    

and in my Main_Activity.java i have onScrollListener where i'am stock, thinking that this is where i can handle the behavior of FAB.

  listView.setOnScrollListener(new AbsListView.OnScrollListener() {              @Override              public void onScrollStateChanged(AbsListView view, int scrollState) {                }              int preLast;              @Override              public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {                  final int lastItem = firstVisibleItem + visibleItemCount;                  if (lastItem > 20) {                      if (preLast != lastItem) {                       // SHow FAB Here                      }                  } else {                      // Hide FAB Here                  }              }            });    

I hope you guys understand my problem. Thank You Very Much! Any Help Would Be Appreciated!

No comments:

Post a Comment