您的位置:首页 > 理论基础 > 计算机网络

SwipeRefreshLayout+AsyncHttpClient 实现异步Http请求的下拉刷新

2016-03-10 14:12 531 查看
感谢原文作者:http://stormzhang.github.io/android/2014/03/29/android-swiperefreshlayout/

API doc:http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

首先须把你的support library的版本升级到19.1或更新

界面:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="@string/swipe_to_refresh"
android:textSize="20sp"
android:textStyle="bold" />
</ScrollView>

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


逻辑代码:

tv = (TextView)findViewById(R.id.textView1);
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
//设置刷新时动画的颜色,可以设置4个
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

@Override
public void onRefresh() {
tv.setText("正在刷新");
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
tv.setText("刷新完成");
swipeRefreshLayout.setRefreshing(false);
}
}, 6000);


可以结合异步AsyncHttpClient请求一起使用,在其onSuccess中调用
swipeRefreshLayout.setRefreshing(false);
方法来向用户提示刷新完成,亲测有效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: