Android系统自带的下拉刷新控件SwipeRefreshLayout

在布局中加入 SwipeRefreshLayout  :

    

        
    

实现接口 SwipeRefreshLayout.OnRefreshListener 并重写 onRefresh 方法,代码如下:

public class MyActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    SwipeRefreshLayout swipeRefreshLayout;
    WebView webView;
    String url = "https://mp.csdn.net/";

    @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);//取消刷新
        }
    }
}

这样就可简单使用系统自己的下拉控件了。

你可能感兴趣的:(控件的基本设置)