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);
}
}
}