swipeRefreshLayout

SwipeRefreshLayout 下拉刷新

2016-08-06  本文已影响89人  TTTqiu

一、布局

---
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ttt.refreshtest.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView>

    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
  1. SwipeRefreshLayout 只能有一个子控件
  2. SwipeRefreshLayout 的子控件必须是 ListView、ScrollView 等这类可以滑动的,不然没有下拉效果。

二、代码


package com.example.ttt.refreshtest;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
    private String[] data=new String[]{"1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=(ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);

        swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipe_refresh);
        // 设置小圆圈的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
//        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.RED);
        // 设置刷新监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 为演示设置一个5秒定时器(还是在主线程)
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 取消掉小圆圈,不然会一直显示
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },5000);
            }
        });
    }
}
上一篇下一篇

猜你喜欢

热点阅读