13

Tôi có một AppBarLayout và NestedScrollView. Tôi muốn các NestedScrollView bất cứ khi nào nó di chuyển xuống, AppBarLayout cũng nên mở rộng duyên dáng, mà không có NestedScrollView dừng ngay trước khi mở rộng AppBarLayout; Chuyến bay/Cuộn thứ hai là bắt buộc để thực hiện điều đó.Fling trơn tru AppBarLayout với NestedScrollView sử dụng AppBarLayout.Behavior

Tôi kiểm tra luồng lưu trữ và tìm thấy giải pháp này khá liên quan và có thể được sử dụng. Nhưng thay vào đó nếu NestedScrollView, nó là RecyclerView. Đó là trong https://stackoverflow.com/a/32454407/3286489

Tôi về cơ bản lấy mã và thay đổi nó một chút và được sử dụng để kiểm tra vận tốc> 8000 để xem xét cũng Fling AppBarLayout dưới dạng mã bên dưới.

public final class FlingBehavior extends AppBarLayout.Behavior { 
    private boolean isPositive; 

    public FlingBehavior() { 
    } 

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

    @Override 
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) { 
     if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) { 
      velocityY = velocityY * -1; 
     } 

     if (target instanceof NestedScrollView && Math.abs(velocityY) > 8000) { 
      consumed = false; 
     } 
     return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed); 
    } 

    @Override 
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) { 
     super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed); 
     isPositive = dy > 0; 
    } 
} 

Điều này có tác dụng nhưng không lý tưởng. Tôi chỉ muốn bắt đầu (tiếp tục) các Fling trên AppBarLayout (tức là trở về consumed = false), khi NestedScrollView đã đạt đến đỉnh của cuộn của nó. Làm thế nào tôi có thể kiểm tra xem trong onNestedFling?

Cảm ơn.

+0

Xin chào đã làm bạn có bất kỳ giải pháp cho việc này .. ?? –

+0

Không. Chỉ trên mỗi giải pháp của tôi ở trên. Có lẽ nhu cầu đánh giá cao hơn về câu hỏi sẽ thu hút một số sự chú ý để giải quyết vấn đề này? – Elye

+0

yea chính xác tôi đã đăng gần như cùng một câu hỏi ở đây ..http: //stackoverflow.com/questions/38119661/fling-with-nestedscrollview-and-appbarlayout –

Trả lời

1

Bạn nên Check for NestedScrollView Và NestedScrollingChild

if (target instanceof NestedScrollView && Math.abs(velocityY) > 8000) { 
     consumed = false; 
    } 


    if (target instanceof NestedScrollingChild && Math.abs(velocityY) > 8000) { 
     consumed = false; 
    } 
Các vấn đề liên quan