【问题标题】:WebView Swipe-to-Refresh Issue: Scrolling up triggers a refreshWebView Swipe-to-Refresh 问题:向上滚动触发刷新
【发布时间】:2019-10-19 08:20:17
【问题描述】:

当我在我的 webview 应用程序中向上滚动时,它会触发滑动刷新刷新- 如何使刷新仅在到达页面顶部时触发? 我看到this 发布了关于这个确切问题的帖子,但我不明白在哪里插入代码 如果您回答,请具体说明我如何实现代码以及在哪里 谢谢

【问题讨论】:

  • onStart 中添加addOnScrollChangedListener,然后在onStop 中添加removeOnScrollChangedListener。如果您想要更具体的答案,请在您使用 WebView 的地方发布您的代码
  • @MohamedAbdelraZek 是的,我在描述中提到了这篇文章

标签: android android-studio android-webview swiperefreshlayout


【解决方案1】:

如果您只使用 WebView,

您可能必须按照提供的顺序使用这些布局,

  1. 滑动刷新布局 1.1 嵌套滚动视图 1.1.1 网页视图
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <WebView
        android:id="@+id/webview1"
        android:layout_width="598dp"
        android:layout_height="1022dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

在你的活动中

public class WebView1 extends AppCompatActivity {

    SwipeRefreshLayout swipeRefreshLayout;
    WebView browser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        browser = (WebView) findViewById(R.id.webview1);
        swipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
        browser.setWebViewClient(new WebViewClient());browser.getSettings().setJavaScriptEnabled(true);


        browser.loadUrl("http://www.google.com");

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                browser.reload();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}

如果您要复制此代码,请注意我正在导入 androidx 库

要使其支持库,请考虑以下事项

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout/>
<androidx.nesv4.widget.NestedScrollView/>

<android.support.v4.widget.SwipeRefreshLayout/>
<android.support.v4.widget.NestedScrollView/>

【讨论】:

    猜你喜欢
    • 2014-09-08
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2016-05-23
    • 1970-01-01
    • 2015-04-11
    相关资源
    最近更新 更多