我试图SwipeRefreshLayout
在我的 WebView 应用程序中实现 android。当我向下滑动时,它会刷新页面,但除了滑动之外它不允许向上和向下滚动。
我已经尝试了一些我在网上找到的基本修复方法,但它仍然无法正常工作。
private WebView mywebview, handshakemywebview;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = findViewById(R.id.MainWebInterface);
handshakemywebview = findViewById(R.id.HandShakeWebInterface);
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mywebview.loadUrl("https://example.com");
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
shopwebview.reload();
}
}
);
}
@Override
protected void onRestart(){
super.onRestart();
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mywebview.getScrollY() == 0) {
mySwipeRefreshLayout.setEnabled(true);
} else {
mySwipeRefreshLayout.setEnabled(false);
}
}
});
}
@Override
protected void onPause(){
super.onPause();
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
}
布局 XML
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/colorPrimary"
android:contentDescription="@string/activityLabel"
android:visibility="visible"
android:id="@+id/MainWebInterface"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<WebView
android:id="@+id/HandShakeWebInterface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/colorPrimary"
android:visibility="gone" />
</RelativeLayout>