簡體   English   中英

SwipeRefreshLayout與WebView中的向下滾動沖突

[英]SwipeRefreshLayout conflict with scroll down in WebView

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

您能幫助我了解如何分開這兩個滾動功能嗎?

編輯:看這里這不能回答我的問題

使用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.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM