android - Custom CoordinatorLayout behavior -


this layout:

<android.support.design.widget.coordinatorlayout    android:id="@+id/coordinator_layout"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">  <include layout="@layout/view_product_details"/>  // contains nestedscrollview  <linearlayout android:id="@+id/indicator"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:layout_gravity="right|bottom"               android:layout_marginbottom="16dp"               android:gravity="right|bottom"               android:orientation="horizontal"               app:layout_behavior="utils.custombehavior">              // views..  </linearlayout> 

i want indicator layout shown when user opens activity should hide when user scrolls bit down.
when user scrolls top of page want show again.

so wrote custom behavior class:

public class custombehavior extends coordinatorlayout.behavior {  private int totaly;  public custombehavior(context context, attributeset attrs) {     super(); }  @override public boolean layoutdependson(coordinatorlayout parent, view child, view dependency) {     return dependency instanceof nestedscrollview; }  @override public boolean onstartnestedscroll(coordinatorlayout coordinatorlayout, view child, view directtargetchild, view target, int nestedscrollaxes) {     return nestedscrollaxes == viewcompat.scroll_axis_vertical || super.onstartnestedscroll(coordinatorlayout, child, directtargetchild, target, nestedscrollaxes); }  @override public void onnestedscroll(coordinatorlayout coordinatorlayout, view child, view target, int dxconsumed, int dyconsumed, int dxunconsumed, int dyunconsumed) {     super.onnestedscroll(coordinatorlayout, child, target, dxconsumed, dyconsumed, dxunconsumed, dyunconsumed);      totaly += dyconsumed;      log.d("scroll", "total y : " + totaly);      linearlayout fabmenu = (linearlayout) child;     floatingactionbutton wishlistbutton = (floatingactionbutton) fabmenu.findviewbyid(r.id.fab_wishlist);     floatingactionbutton collectionbutton = (floatingactionbutton) fabmenu.findviewbyid(r.id.fab_collection);     floatingactionbutton tastedbutton = (floatingactionbutton) fabmenu.findviewbyid(r.id.fab_tasted_list);      if (totaly > 10) {         wishlistbutton.hide(true);         collectionbutton.hide(true);         tastedbutton.hide(true);     } else if (totaly < 10) {         wishlistbutton.show(true);         collectionbutton.show(true);         tastedbutton.show(true);     } } } 

but adding , subtracting of total y value not reliable.
how can detect user has reached top of nestedscrollview?

or there better way implement this? thanks!

see if following - inside onnestedscroll():

use

 if (target instanceof nestedscrollview) {         final nestedscrollview nestedscrollview = (nestedscrollview) target;         totaly = nestedscrollview.getscrolly();       } 

in place of

totaly += dyconsumed; 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -