SwipeRefreshLayout下拉刷新

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.goodness.test.MainActivity">

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

        <EditText
            android:id="@+id/et"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:hint="@string/app_name"
            android:textColor="@color/colorAccent"
            android:textColorHint="@color/colorAccent"
            android:textSize="50sp" />
    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>
package com.goodness.test;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    EditText editText;
    SwipeRefreshLayout swipeRefreshLayout;

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

        editText = (EditText) findViewById(R.id.et);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sw);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {

        swipeRefreshLayout.setRefreshing(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int random = (int) (Math.random() * 100 + 1);
                swipeRefreshLayout.setRefreshing(false);
                //每次刷新显示不同的随机数
                editText.setText(String.valueOf(random));
            }
        }, 1000);
    }
}
原文地址:https://www.cnblogs.com/thankyouGod/p/6403648.html