1

중첩 스크롤 스크롤에서 두 개의 FAB를 숨기려고합니다. 내 자신의 FAB 동작을 구현하려고했지만 작동하지 않습니다. FAB은 스크롤하는 것에 전혀 반응하지 않습니다. 참고 : 게시물의 일부 레이아웃을 삭제했습니다.중첩 스크롤에 플로팅 동작 단추를 숨기는 방법

레이아웃

<?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="ru.berezin.maxim.im.budget_v3.userinterface.StartPage"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appcollapse" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/testingscroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     app:behavior_overlapTop="30dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <!--content including--> 
     <include layout="@layout/start_content_layout"/> 

    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/start_page_fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="16dp" 
     android:src="@drawable/plus" 
     app:layout_anchor="@id/testingscroll" 
     app:layout_anchorGravity="end|bottom" 
     app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll" 
     /> 

    <View 
     android:id="@+id/dummy" 
     android:layout_width="1dp" 
     android:layout_height="16dp" 
     app:layout_anchor="@id/start_page_fab" 
     app:layout_anchorGravity="top|right|end" 
     /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/account_det_add_transfer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|top" 
     android:layout_margin="16dp" 
     android:src="@drawable/minus" 
     app:layout_anchor="@id/dummy" 
     app:layout_anchorGravity="top|right|end" 
     app:layout_behavior="ru.berezin.maxim.im.budget_v3.FabOnScroll" 
     /> 
</android.support.design.widget.CoordinatorLayout> 

FabOnScroll 클래스

public class FabOnScroll extends FloatingActionButton.Behavior { 
     public FabOnScroll(Context context, AttributeSet attrs) { 
      super(); 
     } 

     @Override 
     public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { 
      super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); 

      //child -> Floating Action Button 
      if (dyConsumed > 0) { 
       CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); 
       int fab_bottomMargin = layoutParams.bottomMargin; 
       child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start(); 
      } else if (dyConsumed < 0) { 
       child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start(); 
      } 
     } 

     @Override 
     public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { 
      return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; 
     } 

    } 

답변

1

그래서, 나는 그것을 해결했습니다. 정확히 내가 원하는 것은 아니지만 작동합니다. 왜 그런지 모르겠지만 어떤 이유로 dyConsumed는 항상 0입니다. 그러나 스크롤하는 동안 dyUnconsumed가 변경되는 것을 확인했습니다. 그래서 나는 If/else statment에 dxUnconsumed check를 추가했습니다. 누군가 dyConsumed가 0과 같은 이유를 설명 할 수 있습니까?

0
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{ 
@Override 
public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{ 
    if (dy > 0 ||dy<0 && fab.isShown()) 
    { 
     fab.hide(); 
    } 
} 

@Override 
public void onScrollStateChanged(RecyclerView recyclerView, int newState) 
{ 
    if (newState == RecyclerView.SCROLL_STATE_IDLE) 
    { 
     fab.show(); 
    } 

    super.onScrollStateChanged(recyclerView, newState); 
} 
}); 
+0

나는 recyclerView가 없습니다. NestedScroll에 텍스트가 포함 된 cardViews가 포함되어 있습니다. – Kroha

+0

코드를 업데이트하십시오. 희망이 그것이 작동합니다 –

+0

if (dyConsumed> 0 && child.getVisibility() == View.VISIBLE) { child.hide(); } else if (dyConsumed <0 && child.getVisibility() == View.GONE) { child.show(); } –

1

아래의 코드를 NestedScrollView ScrollChangeListener에 사용해보고 이것이 예상하고 있는지 확인하십시오. RecyclerView 대신 NestedScrollView를 사용하는 사용자에게 적합해야합니다.

NestedScrollView nsv = (NestedScrollView) v.findViewById(R.id.nsv); 
    nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { 
     @Override 
     public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
      if (scrollY > oldScrollY) { 
       fab.hide(); 
      } else { 
       fab.show(); 
      } 
     } 
    });