2016-12-14 20 views
20

tôi đã cố gắng cập nhật com.android.support:appcompatcom.android.support:design từ: 25.0.1 đến 25.1.0, như sau:Floating nút hành động không hiển thị trên di chuyển sau khi cập nhật Google Hỗ trợ & Design Library

compile 'com.android.support:appcompat-v7:25.0.1' 
compile 'com.android.support:design:25.0.1' 

tới:

compile 'com.android.support:appcompat-v7:25.1.0' 
compile 'com.android.support:design:25.1.0' 

nhưng tôi thấy rằng nút tác vụ nổi của tôi không còn xuất hiện khi hoạt động cuộn. hành vi FAB của tôi được xác định bởi những điều sau đây:

public class MyFabBehavior extends FloatingActionButton.Behavior { 

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

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, 
             FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { 

     // Ensure we react to vertical scrolling 
     return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL 
       || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); 

    } 

    @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); 
     if (dyConsumed < 0) { 
      // User scrolled up -> hide the FAB 
      animateFab(child, View.GONE); 
     } else if (dyConsumed > 0) { 
      // User scrolled down -> show the FAB 
      animateFab(child, View.VISIBLE); 
     } 
    } 

    static public void animateFab(FloatingActionButton fab, int visibility) { 
     // ignore visibility passed in, and just make fab visible regardless 
     if (fab.getVisibility() != View.VISIBLE) { 
      fab.show(); 
     } 
    } 
} 

và bố trí của tôi là như sau:

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/main_scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="8dp" > 

     ... 

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

    <android.support.design.widget.FloatingActionButton 
     app:layout_behavior="com.example.MyFabBehavior" 
     android:id="@+id/fab" 
     app:fabSize="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_marginBottom="@dimen/fab_margin" 
     android:layout_marginRight="@dimen/fab_margin" 
     android:onClick="saveButton" 
     app:elevation="6dp" 
     app:pressedTranslationZ="12dp" 
     app:backgroundTint="@color/colorPrimary" 
     android:src="@drawable/ic_done_white_24dp" /> 

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

Trả lời

1

trong CoordinatorLayout 25.1.0 (

for (int i = 0; i < childCount; i++) { 
      final View view = getChildAt(i); 
      if (view.getVisibility() == GONE) { 
       // If the child is GONE, skip... 
       continue; 
      } 

trong 25.0.1

for (int i = 0; i < childCount; i++) { 
      final View view = getChildAt(i); 
      final LayoutParams lp = (LayoutParams) view.getLayoutParams(); 
      if (!lp.isNestedScrollAccepted()) { 
       continue; 
      } 

      final Behavior viewBehavior = lp.getBehavior(); 
      if (viewBehavior != null) { 
       viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed, 
         dxUnconsumed, dyUnconsumed); 
       accepted = true; 

     } 
+0

Trong hộp thoại 'onCreate()' của 'Activity' tôi Tôi gọi 'hide() 'trên' FloatingActionButton', để nó bắt đầu ẩn và sau đó xuất hiện trên cuộn (thông qua hành vi đã xác định).Việc gọi 'hide()' có đặt hiển thị của nó thành 'GONE' và đoạn mã của bạn có cho thấy rằng nút hành động thả nổi được bỏ qua mãi mãi trong các sự kiện cuộn không? Chắc chắn không? – drmrbrewer

53

Cập nhật từ thư viện hỗ trợ 25.0.1 đến 25.1.0 thay đổi phương thức onNestedScroll của CoordinatorLayout trong đó cuộc gọi bị bỏ qua cho chế độ xem có chế độ hiển thị được đặt thành View.GONE.

Calling child.hide() vào nút hành động nổi đặt tầm nhìn của quan điểm để View.GONE, mà bây giờ có nghĩa là (tính 25.1.0), cuộc gọi onNestedScroll phương pháp sẽ bị bỏ qua cho nút hành động nổi trong tương lai (vì nó bỏ qua tất cả các quan điểm có khả năng hiển thị là GONE).

Giải pháp cho việc này là đặt chế độ hiển thị của chế độ xem thành INVISIBLE bất cứ khi nào bạn ẩn. Bằng cách này, onNestedScroll sẽ không bỏ qua chế độ xem vào lần tiếp theo khi cuộn lồng nhau được thực hiện.

Để đạt được điều này, bạn có thể gọi

child.hide(new FloatingActionButton.OnVisibilityChangedListener() { 
      /** 
      * Called when a FloatingActionButton has been hidden 
      * 
      * @param fab the FloatingActionButton that was hidden. 
      */ 
      @Override 
      public void onHidden(FloatingActionButton fab) { 
       super.onShown(fab); 
       fab.setVisibility(View.INVISIBLE); 
      } 
     }); 

trong phương pháp onNestedScroll của bạn.

Edit: Vấn đề này đã được đệ trình lên Issue Tracker AOSP tại https://code.google.com/p/android/issues/detail?id=230298

+5

Bạn đã cứu tôi công việc của tôi thưa ông. Cảm ơn bạn! –

+1

Wow điều này là điên rồ !, chúng tôi đã tìm thấy cùng một vấn đề khi cập nhật lên 25.1.1 ', cảm ơn đề xuất. – Jorgesys

+1

Có vẻ như một lỗi đánh máy bên trong phương pháp ghi đè. Nên là 'super.onHidden (fab) 'tôi nghĩ vậy. – Anthonyeef

0

Các hành vi đã thay đổi kể từ hỗ trợ lib phiên bản 25.1.0.

Nó phải là RecyclerView (Hành vi) kích hoạt thay đổi mức độ hiển thị FAB.

Nói cách khác, nó không còn là trách nhiệm của đối tượng muốn phản ứng lại để có hành vi mà là đối tượng di chuyển để nhận biết mọi thứ trên màn hình.

Dưới đây là một liên kết đến một diff cho thấy những thay đổi được yêu cầu để thực hiện nâng cấp:

https://github.com/chrisbanes/cheesesquare/compare/master...ianhanniballake:scroll_aware_fab

Các vấn đề liên quan