Android--SwipeRefreshLayout下拉刷新Demo

1.简介

1.Android-v4包中出现的新控件

2.使用方式

1.xml布局文件:

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    tools:context="com.longshun.animationdemo.customview.SwipeRefreshDemo"
        >


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

</android.support.v4.widget.SwipeRefreshLayout>

2.Activity中java代码:

package com.longshun.animationdemo.customview;

import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;


public class SwipeRefreshDemo extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_refresh_demo);
        //刷新控件
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

        ListView view = (ListView) findViewById(R.id.swipe_listview);
        ArrayList<String> strings = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            strings.add("string "+i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);
        view.setAdapter(adapter);

        //设置加载刷新时的回调接口
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        //设置刷新动画
        refreshLayout.setRefreshing(true);
        //TODO something you want to do
        //如 开启异步任务加载网络数据
        new MyTask().execute();
    }
    private class MyTask extends AsyncTask<String,Integer,String>{
        @Override
        protected String doInBackground(String... params) {
            try {
            //demo中不加载网络数据了
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            //数据加载完毕,取消刷新动画
            refreshLayout.setRefreshing(false);
            super.onPostExecute(s);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值