SwipeRefreshLayout与WebView中的向下滚动冲突

时间:2018-06-13 11:38:46

标签: android webview scroll refresh pull-to-refresh

在我的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>

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

编辑: 看着here这不回答我的问题

1 个答案:

答案 0 :(得分: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.
    }
}