WebView刷新后刷新:向上滚动会触发刷新

问题描述 投票:0回答:1

[当我在Webview应用中向上滚动时,它会触发“刷卡刷新”刷新,如何仅在到达页面顶部时才进行刷新以触发?我看到this发布了有关此确切问题的信息,但我不明白该在哪里插入代码如果您回答,请具体说明我如何实现代码以及在何处实现谢谢

android android-studio android-webview swiperefreshlayout
1个回答
0
投票

如果仅使用WebView,则>

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

  1. SwipeRefreshLayout1.1 NestedScrollView1.1.1 WebView
  2. <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/>

to

<android.support.v4.widget.SwipeRefreshLayout/>
<android.support.v4.widget.NestedScrollView/>
© www.soinside.com 2019 - 2024. All rights reserved.