2016-01-10 43 views
3

我有一個Fragment,它有一個ScrollView和一個SwipeRefreshLayout。當ScrollView不在頂部時,我想禁用SwipeRefreshLayout,因此用戶可以再次在Fragment的頂部滾動。我試圖創建一個自定義ScrollView並重寫onScrollChanged方法這樣當ScrollView不在頂部時禁用SwipeRefreshLayout

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
    View view = (View) getChildAt(getChildCount() - 1); 
    int d = view.getBottom(); 
    d -= (getHeight() + getScrollY()); 
    if (d <= 0) { 
     // bottom 
    } else if (getScrollY() <= 0) { 
     // top 
     swipeRefreshLayout.setEnabled(true); 
    } else { 
     swipeRefreshLayout.setEnabled(false); 
     super.onScrollChanged(l, t, oldl, oldt); 
    } 
} 

這適用於人像模式很好,但不是橫向模式。我也試着檢查是否getScrollX() <= 0但它沒有工作。

+0

如果用戶不在ScrollView的頂部,用戶如何觸發刷新? – jimmy0251

+0

他回到頂部並滑動。 –

回答

0

嘗試使用NestedScrollView

當需要在另一個滾動視圖內滾動視圖時使用。