SwipeRefreshLayout与WebView下拉滚动冲突

4
在我的WebView应用程序中,SwipeRefreshLayout无法使用,因为当我向下滚动时,它会触发页面重新加载函数。我认为一个解决方案是限制下拉刷新布局。我尝试了这个方法,但似乎并没有起作用。
以下是我的实现:
Java文件
SwipeRefreshLayout mySwipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

String URL = "my_url";
mWebView = (WebView) findViewById(R.id.enyakin);
WebSettings webSettings = mWebView.getSettings();
mWebView.loadUrl(URL);
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {

    // Phone call function if this matters
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.startsWith("tel:"))
        {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        view.loadUrl(url);
        return true;
    }
});

mySwipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mWebView.reload();
                if (null != mySwipeRefreshLayout) {
                    mySwipeRefreshLayout.setRefreshing(false);
                }
            }
        }
);

XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.google.firebase.quickstart.fcm.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:fillViewport="true">
            <WebView
                android:id="@+id/enyakin"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

您能帮我理解如何分离这两个滚动功能吗?

编辑: 在这里看过,但没有回答我的问题。


可能是在WebView上添加下拉刷新以进行刷新的重复问题。 - MSpeed
@MikeSpeed 没有解决方案。 - Abdullah Akçam
1个回答

0

使用setOnChildScrollUpCallback来控制何时触发刷新。

这是Kotlin的示例代码。

mySwipeRefreshLayout.apply {
    setOnRefreshListener {
        // Handle refresh
    }

    setOnChildScrollUpCallback {parent, child ->
        // Return true to disable swiping to refresh. 
        // You can get scrolling position of nested scroll view and only return true when it is larger than 0.
    }
}

setOnChildScrollUpCallback 重复了,请分享完整的代码。 - Md Imran Choudhury

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接