说到下拉刷新,最为人所知的就是chrisbanes的Android-PullToRefresh,一个强大的下拉刷新开源项目,支持各种常用控件。后来,Google为我们提供的android-support-v4包,从19.1版本开始,推出了自己的下拉刷新控件SwipeRefreshLayout。在知乎和豌豆荚等应用中我们都可以看到SwipeRefreshLayout的影子。
SwipeRefreshLayout官方文档链接:
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
下面,我们来实现一个简单的下拉刷新操作。
效果图:
核心代码
MainActivity.java
SwipeRefreshLayout官方文档链接:
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
下面,我们来实现一个简单的下拉刷新操作。
效果图:
核心代码
MainActivity.java
package net.csdn.blog.ruancoder;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshLayout;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
List<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("item " + i);
}
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,
data);
listView.setAdapter(mAdapter);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);
// 设置刷新时进度动画变换的颜色,接收参数为可变长度数组。也可以使用setColorSchemeColors()方法。
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_orange_light, android.R
.color.holo_green_light);
// 设置刷新时圆形图标的大小。只可传递2个参数,SwipeRefreshLayout.LARGE或SwipeRefreshLayout.DEFAULT。
mSwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
// 设置刷新时圆形图标的背景色。也可以使用setProgressBackgroundColorSchemeColor()方法。
mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.background_light);
// 设置刷新时的圆形图标。scale:是否缩放;start:圆形图标在刷新前的起始位置;end:圆形图标的偏移量。
mSwipeRefreshLayout.setProgressViewOffset(true, 100, 200);
// 设置会触发下拉刷新的手势滑动距离
mSwipeRefreshLayout.setDistanceToTriggerSync(100);
// 设置刷新的监听事件
mSwipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {// 监听下拉刷新
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.add("blog.csdn.net/ruancoder");
// 结束刷新
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>