简单的使用SwipeRefreshLayout

来源:互联网 发布:视频后期编辑软件 编辑:程序博客网 时间:2024/06/06 05:27

SwipeRefreshLayout

google提供的一个可实现下拉刷新效果的widget,该控件有一下几个常用的方法:

 1. setOnRefreshListener 添加下拉刷新的监听 2. setColorSchemeResources 设置进度条的颜色(最多设置四种颜色) 3. setRefreshing 显示或者隐藏进度条 4. isRefreshing 检测是否正在执行刷新操作

以下是代码示例

<android.support.v4.widget.SwipeRefreshLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <ScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:id="@+id/pull_refresh_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:layout_margin="10dp"            android:textColor="#201f1f"            android:textSize="18sp"            android:text="下拉刷新"/>    </ScrollView></android.support.v4.widget.SwipeRefreshLayout>
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 绑定组件        final TextView refreshTv = (TextView) findViewById(R.id.pull_refresh_tv);        final SwipeRefreshLayout mainLayout = (SwipeRefreshLayout)findViewById(R.id.activity_main);        // 设置刷新时的颜色        mainLayout.setColorSchemeResources(R.color.blue,R.color.red,R.color.orange,R.color.green);        // 设置刷新监听        mainLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @Override            public void onRefresh() {                // 当文字下拉的时候,改变文字                refreshTv.setText("正在刷新");                // 做一个延时操作,模拟刷新过程                new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        refreshTv.setText("刷新完成");                        mainLayout.setRefreshing(false);                    }                    // 5000毫秒(5秒)后调用Run方法                },5000);            }        });    }}
0 0
原创粉丝点击