【问题标题】:scrolling up in webview causes refresh by SwipeRefreshLayout在 webview 中向上滚动会导致 SwipeRefreshLayout 刷新
【发布时间】:2015-10-03 10:53:57
【问题描述】:

我在 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】:

只需使用 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);
    }
}

【讨论】:

    【解决方案2】:

    没有解决方案对我有用。这是我的方法。

     @Override
     public boolean dispatchTouchEvent(MotionEvent event)
     {
    super.dispatchTouchEvent(event);
    int x = (int)event.getX();
    int y = (int)event.getY();
     if(y>800){
         swipeRefreshLayout.setEnabled(false);
     }else {
         swipeRefreshLayout.setEnabled(true);
     }
    
    
    return false;
    }
    

    【讨论】:

    • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多