activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lt.an26_swiperefreshlayout.MainActivity">
<!--直接的子view只能有一个-->
<!--特点与scrollview一样-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:textSize="20sp"
android:textStyle="bold"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity部分
package com.lt.an26_swiperefreshlayout;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
final TextView tvMsg = (TextView) findViewById(R.id.tv_msg);
assert refreshLayout != null;
//设置进度条的变化颜色
refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_red_light, android.R.color.holo_orange_light);
//设置进度圆圈的大小:默认值DEFAULE和大的值LAREG
refreshLayout.setSize(SwipeRefreshLayout.LARGE);
//设置进度圈的背景色,一般不用,除非个人爱好
//refreshLayout.setProgressBackgroundColorSchemeColor(Color.GRAY);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//下拉刷新
@Override
public void onRefresh() {
//在这里执行加载数据的任务
assert tvMsg != null;
tvMsg.setText("拼命加载中....");
tvMsg.setTextColor(Color.BLUE);
//延迟执行某任务
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
tvMsg.setText("加载完成...");
//设置进度条的显示状态
refreshLayout.setRefreshing(false);
}
}, 5000);
}
});
}
}
运行效果图