android SwipeRefreshLayout不';不允许Web视图滚动
我正在尝试在我的WebView应用程序中实现androidandroid SwipeRefreshLayout不';不允许Web视图滚动,android,android-layout,android-webview,androidx,android-swipe,Android,Android Layout,Android Webview,Androidx,Android Swipe,我正在尝试在我的WebView应用程序中实现androidSwipeRefreshLayout。当我向下滑动时,它会刷新页面,但除了滑动之外,它不允许再上下滚动 我尝试了一些我在网上找到的基本修复方法,但仍然不起作用 private WebView mywebview, handshakemywebview; private SwipeRefreshLayout mySwipeRefreshLayout; private ViewTreeObserver.OnScrollChangedListe
SwipeRefreshLayout
。当我向下滑动时,它会刷新页面,但除了滑动之外,它不允许再上下滚动
我尝试了一些我在网上找到的基本修复方法,但仍然不起作用
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>
在onResume中写入OnRestart代码。
这是因为您在重新启动时添加了addOnScrollChangedListener,而onRestart并不总是调用在swiperefreshlayout中添加NestedScrollView。您可以像这样将webview包装在滑动参照布局中
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/MainWebInterface"
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" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
我刚刚这么做了,但仍然一样,它允许我向下滚动,但当我尝试向上滚动时,它不允许work@PeterSmith为什么所有组件的布局高度都与父级匹配?只要看一看,似乎您可以通过在滚动视图中包装RelativeLayout
并将所有其他组件的高度设置为wrap\u content
来解决此问题。