在布局中加入 SwipeRefreshLayout :
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
实现接口 SwipeRefreshLayout.OnRefreshListener 并重写 onRefresh 方法,代码如下:
public class MyActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeRefreshLayout;
WebView webView;
";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
webView = findViewById(R.id.webView);
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
//设置下拉刷新的箭头颜色
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light);
//设置下拉刷新的背景颜色为白色
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
swipeRefreshLayout.setOnRefreshListener(this);
//加载网址
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl(url);
}
@Override
public void onRefresh() {
//下拉刷新要执行的操作
webView.loadUrl(url);
if (swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);//取消刷新
}
}
}
这样就可简单使用系统自己的下拉控件了。