下拉刷新--第三方开源--PullToRefresh
2015-11-24 18:47
579 查看
原博:http://home.cnblogs.com/u/zzw1994/
效果预览图:
下载地址:https://github.com/chrisbanes/Android-PullToRefresh
activity_main.xml:
MainActuvity.java:
效果预览图:
下载地址:https://github.com/chrisbanes/Android-PullToRefresh
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.testpulltorefresh.MainActivity" > 6 7 <com.handmark.pulltorefresh.library.PullToRefreshListView 8 android:id="@+id/listView" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" /> 11 12 </RelativeLayout>
MainActuvity.java:
1 package com.zzw.testpulltorefresh; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 6 import com.handmark.pulltorefresh.library.PullToRefreshBase; 7 import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; 8 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; 9 import com.handmark.pulltorefresh.library.PullToRefreshListView; 10 11 import android.app.Activity; 12 import android.os.AsyncTask; 13 import android.os.Bundle; 14 import android.os.SystemClock; 15 import android.widget.Adapter; 16 import android.widget.ArrayAdapter; 17 import android.widget.ListView; 18 import android.widget.TextView; 19 import android.widget.Toast; 20 21 public class MainActivity extends Activity { 22 23 private PullToRefreshListView listView; 24 private ArrayList<String> data; 25 private ArrayAdapter adapter; 26 private int count = 0; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 33 data = new ArrayList<String>(); 34 35 listView = (PullToRefreshListView) findViewById(R.id.listView); 36 // 设置向下滑动时刷新 37 listView.setMode(Mode.PULL_FROM_START); 38 // 设置监听 39 listView.setOnRefreshListener(new OnRefreshListener<ListView>() { 40 41 @Override 42 public void onRefresh(PullToRefreshBase<ListView> refreshView) { 43 // 在这完成业务逻辑 44 new MyAsyncTask().execute(); 45 } 46 }); 47 48 adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data); 49 listView.setAdapter(adapter); 50 51 // 设置如果数据为空的时候显示什么 52 TextView textView = new TextView(this); 53 textView.setText("请下拉刷新"); 54 listView.setEmptyView(textView); 55 } 56 57 private class MyAsyncTask extends AsyncTask { 58 59 @Override 60 protected void onPreExecute() { 61 // 开始刷新 62 listView.setRefreshing(); 63 } 64 65 @Override 66 protected Object doInBackground(Object... params) { 67 // 假设耗时时间为3秒 68 SystemClock.sleep(3000); 69 return count++; 70 } 71 72 @Override 73 protected void onPostExecute(Object result) { 74 75 data.add(0, result + ""); 76 adapter.notifyDataSetChanged(); 77 78 // 设置标签 79 listView.setLastUpdatedLabel("最后更新新的时间:" + new Date()); 80 81 // 刷新完成 82 listView.onRefreshComplete(); 83 Toast.makeText(getApplicationContext(), "加载成功", 0).show(); 84 } 85 } 86 87 }
相关文章推荐
- php7 vs HHVM
- OpenCV人形检测Hog
- webapp开发要点记录
- invalid use of incomplete type 'class-Ui-Widge't的解决办法
- django中forloop
- 在eclipse的工具栏中不显示Android SDK and AVD Manager
- Tui-x 自适应屏幕 (转) ----- 6
- Skype For Business2015 监控-存档服务器配置介绍
- Java String.split()用法小结
- 建设产业,才能广获丰收--建广资本收购NXP RFPA部门的再解析
- 在AIX中查看文件大小
- 上海办理房产税变更
- 绘制y=sin(x)/x的图形
- Head First Android Development-笔记
- 一键安装metasploit(linux,os x)
- No18-【原】跟我一起来了解hexo的结构,自定义一些组件
- Tui-x 运行demo和部署项目 ( 转 ) ----- 5
- Tui-x 资源管理( 转 ) ----- 4
- IOS的生命周期及应用的前后台切换
- spi协议及工作原理分析