andriod PullToRefresh的使用 构建下拉刷新的listView
来源:互联网 发布:html时间轴 demo 源码 编辑:程序博客网 时间:2024/05/20 00:10
下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;
1.导入Library
2.布局xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><!-- The PullToRefreshListView replaces a standard ListView widget. --> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrMode="both" //上下都能刷新 /></LinearLayout>
3.java代码
package com.example.pulltoflush;import java.util.ArrayList;import java.util.List;import java.util.Set;import java.util.TreeSet;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshListView;import android.os.AsyncTask;import android.os.Bundle; import android.app.Activity; import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity { private PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mArrayAdpter; private List<String> mArraylist=new ArrayList<String>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); mArrayAdpter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,mArraylist); mPullRefreshListView.setAdapter(mArrayAdpter); mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {new GetDataTask().execute();} }); } private class GetDataTask extends AsyncTask<Void, Void, String> {protected String doInBackground(Void... arg0){try {Thread.sleep(2000);} catch (InterruptedException e) {} return shuangsheqiu();} protected void onPostExecute(String result) {mArraylist.add(result);mArrayAdpter.notifyDataSetChanged();mPullRefreshListView.onRefreshComplete();super.onPostExecute(result); }} private String shuangsheqiu(){// TODO Auto-generated method stubSet<String> treeSet=new TreeSet<String>();do{treeSet.add(getredball()); } while (treeSet.size()!=6);String[] arrayString=new String[6];arrayString=treeSet.toArray(arrayString);StringBuilder resb=new StringBuilder();resb.append("[第"+(mArraylist.size()+1)+"组] ");for(int i=0;i<6;i++){resb.append(arrayString[i]);resb.append("-");}resb.append("+蓝:");resb.append(getbulball()); return resb.toString();}private String getredball(){ int i= (int)(Math.random()*33+1); if(i<10){ return "0"+String.valueOf(i); }else{ return String.valueOf(i); }}private String getbulball(){ int i= (int)(Math.random()*16+1); if(i<10){ return "0"+String.valueOf(i); }else{ return String.valueOf(i); }} }
(1)派生自AsyncTask
由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数
(2)doInBackground函数
doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends AsyncTask<Void, Void, String> 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;
(3)onPostExecute函数
onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mArraylist.addFirst(result);和添加在尾部----
mArraylist<span style="font-family: Arial; background-color: rgb(255, 255, 255);">.addLast(result);</span>
0 0
- andriod PullToRefresh的使用 构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- chrisbanes/Android-PullToRefresh ListView的下拉刷新
- android验证码识别以及判断内容是否为空
- 技术人生:故事之四十四服务时代
- error C2440: “static_cast”: 无法从“void (__thiscall CChatDlg::* )(WPARAM,LPARAM)”转换为“LRESULT (__thisca
- 线性表概念
- Docker命令行探秘
- andriod PullToRefresh的使用 构建下拉刷新的listView
- ORA-00257: archiver error. Connect internal only, until freed 归档日志满处理方法
- java SE复习笔记28
- 可变的圆 二分?排序?
- UVA 11136 Hoax or what(Multiset)
- 技术人生:故事之四十五谁明白为什么信息化
- 技术人生:故事之四十六互联网络给了我们什么
- getParameter与getAttribute的区别
- nnoDB引擎类型支持外键约束