[当我在Webview应用中向上滚动时,它会触发“刷卡刷新”刷新,如何仅在到达页面顶部时才进行刷新以触发?我看到this发布了有关此确切问题的信息,但我不明白该在哪里插入代码如果您回答,请具体说明我如何实现代码以及在何处实现谢谢
如果仅使用WebView,则>
您可能必须按提供的顺序使用这些布局,
<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/>