2017-01-13 2 views
7

Я сделал приложение для Android с recyclerview и floating action button. При прокрутке вниз кнопка должна скрываться, при прокрутке вверх она должна снова отображаться. Я использовал this tutorial для реализации поведения.Scroll aware FAB скрывается, но затем не появляется

В результате, что FAB скрывает, когда я прокручиваю вниз, но при прокрутке он не появляется снова :(Класс ScrollAwareFABBehavior идентичен из учебника. Но я использую вложенные макеты.

Здесь мой макет (recyclerview находится в LinearLayout в content_main):

<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="org.myorganisation.mypackage.someActivity"> 

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

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/add_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/plus" 
     app:layout_behavior="org.myorganisation.mypackage.someActivity.helpers.ScrollAwareFABBehavior" /> 

    <LinearLayout 
     android:id="@+id/insert_alert" 
     android:layout_width="wrap_content" 
     android:layout_height="50sp" 
     android:layout_margin="@dimen/fab_margin" 
     android:gravity="center_vertical" 
     android:orientation="horizontal" 
     android:paddingEnd="70sp" 
     android:visibility="gone" 
     app:layout_anchor="@id/add_fab" 
     app:layout_anchorGravity="bottom|left"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:text="@string/initial_alert" 
      android:textColor="@color/colorPrimaryDark" 
      android:textStyle="bold|italic" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_keyboard_arrow_right_black_24sp" 
      android:tint="@color/colorPrimary" /> 
    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

ответ

3

в версии 25.1.0 библиотеки поддержки, скрытые взгляды больше не прокручивать события, как на this bug

.

Как уже упоминались в comment #5:

Это работает, как задумано, ваша цепочка зависимостей является неправильным путем. Ваш RecyclerView (Поведение) должен запускать изменение видимости FAB.

+1

Спасибо. Уменьшение до '25.0.1' решило это;) – Yonjuni

+1

Вы также можете переключиться на подход, рекомендованный ошибкой - есть пример [здесь] (https: //github.com/chrisbanes/cheesesquare/compare/master ... ianhanniballake: scroll_aware_fab) – ianhanniballake

+0

@ianhanniballake новое решение имеет проблему, оно не скрывает fab, если список заполняет область просмотра. Хотя это, вероятно, желательно, потому что fab может наложить последний элемент в списке. Есть идеи, как это решить? Я хочу, чтобы не добавлять нижний отступ в мой список, чтобы иметь возможность прокручивать содержимое выше fab в этом случае ... – prom85

1

Я решил эту проблему путем замены .setVisibility (View.VISIBLE) с .Show (истина) и .setVisibility (View.GONE) с .hide (истинной):

А вот мой класс для поведения, который работает с 25.1.0: и Clans FloatingActionButton

public class ScrollAwareFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { 
private static boolean mIsAnimatingOut = false; 
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); 

public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { 
    super(); 
} 

@Override 
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fabButton, View dependency) { 
    return dependency instanceof Snackbar.SnackbarLayout; 
} 

@Override 
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fabButton, View dependency) { 
    float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()); 
    fabButton.setTranslationY(translationY); 
    return true; 
} 

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

} 

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

    if (fabButton.isEnabled()) { 
     if (dyConsumed > 0 && !mIsAnimatingOut && fabButton.isShown()) { 
      animateOut(fabButton); 
     } else if ((dyConsumed < 0 || dyUnconsumed < 0) && fabButton.isHidden()) { 
      animateIn(fabButton); 
     } 
    } 
} 

public static void animateOut(final FloatingActionButton fabButton) { 
    if (Build.VERSION.SDK_INT >= 14) { 
     ViewCompat.animate(fabButton).translationY(168F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer() 
       .setListener(new ViewPropertyAnimatorListener() { 
        public void onAnimationStart(View view) { 
         mIsAnimatingOut = true; 
        } 

        public void onAnimationCancel(View view) { 
         mIsAnimatingOut = false; 
        } 

        public void onAnimationEnd(View view) { 
         mIsAnimatingOut = false; 
         fabButton.hide(true); 
        } 
       }).start(); 
    } else { 
     Animation anim = AnimationUtils.loadAnimation(fabButton.getContext(), android.R.anim.fade_in); 
     anim.setInterpolator(INTERPOLATOR); 
     anim.setDuration(200L); 
     anim.setAnimationListener(new Animation.AnimationListener() { 
      public void onAnimationStart(Animation animation) { 
       mIsAnimatingOut = true; 
      } 

      public void onAnimationEnd(Animation animation) { 
       mIsAnimatingOut = false; 
       fabButton.hide(true); 
      } 

      @Override 
      public void onAnimationRepeat(final Animation animation) { 
      } 
     }); 
     fabButton.startAnimation(anim); 
    } 
} 

public static void animateIn(FloatingActionButton fabButton) { 
    fabButton.show(true); 
    if (Build.VERSION.SDK_INT >= 14) { 
     ViewCompat.animate(fabButton).translationY(0).scaleX(1.0F).scaleY(1.0F).alpha(1.0F) 
       .setInterpolator(INTERPOLATOR).withLayer().setListener(null) 
       .start(); 
    } else { 
     Animation anim = AnimationUtils.loadAnimation(fabButton.getContext(), android.R.anim.fade_out); 
     anim.setDuration(200L); 
     anim.setInterpolator(INTERPOLATOR); 
     fabButton.startAnimation(anim); 
    } 
} 

}

2

что касается поддержки версии библиотеки 25.1.0 , решение - here.

Смежные вопросы