天天看點

android之swiperefreshlayout與scrollview沖突解決

android之swiperefreshlayout與scrollview沖突解決

知識點:

1、解決swiperefreshlayout與scrollview的沖突問題。

在開發中,我們會用到新的控件swipeRefreshLayout,但是我們同時又要用到scrollView。我們知道swipeRefreshLayout和scrollView這兩個控件的子控件都必須有且隻有一個的,而且這兩個控件都會檢測使用者滑動的動作,由于事件的分派的問題,就出現了我們所遇到的沖突問題。

下面是我實踐過,可行的解決兩者共用所産生的滾動沖突問題。

原理很簡單:就是在scrollview還沒有到達第一條資料頂部的時候,就設定swipeRefreshLayout為不可操作狀态,那麼檢測swipeRefreshLayout的滾動分發就不起作用了,就達到我們需要的目的了。

if (scrollView != null) {
            scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    if (swipeRefreshLayout != null) {
                        swipeRefreshLayout.setEnabled(scrollView.getScrollY() == 0);
                        CommonLog.infoLog("init scrollView");
                    }
                }
            });
        }
           

如有任何問題,請及時與我聯系,謝謝!