在webview中向上滚动会导致SwipeRefreshLayout刷新

问题描述 投票:11回答:1

我在FrameLayout中有一个WebView,因为我正在创建一个浏览器来添加标签。 FrameLayout位于SwipeRefreshLayout内。

问题:每当我在WebView中快速滚动内容时,工具栏后面会出现刷新图标。它应该只在WebView内容位于顶部时才会发生。它与FrameLayout有关,当我删除它时,问题就消失了。

布局如下所示:

<SwipeRefreshLayout
            android:id="@+id/swipeRefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        <FrameLayout
        android:id="@+id/webViewFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"    
        >
        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        </FrameLayout>

</SwipeRefreshLayout>
android android-webview android-framelayout swiperefreshlayout
1个回答
2
投票

只需使用ViewTreeObserver.OnScrollChangedListener实现Fragment或Activity,然后设置Listener,如webview.getViewTreeObserver()。addOnScrollChangedListener(this);

在onScrollChanged()方法中这样做

@Override
public void onScrollChanged() {
    if (webview.getScrollY() == 0) {
        swipeLayout.setEnabled(true);
    } else {
        swipeLayout.setEnabled(false);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.