前言:上篇我们讲了怎么初步使用PullToRefresh,但上篇只是几个简单的字符串,在真正的项目中,不可能只是这么简单的,而是复杂的XML的累积,这篇我在前一篇和以前讲的simpleAdapter的基础上,进一步实现复杂XML的下拉刷新
相关文章:
(这篇文章是建立在这三篇文章的基础上,其实是在利用了《List控件使用--SimpleAdapter使用详解(二)》的布局和重写BaseAdapter的代码,然后利用了《PullToRefresh使用详解(一)--构建下拉刷新的ListView》的下拉刷新功能。所以文章的部分代码省略没讲,强烈建议大家先看这三篇。)
1、《List控件使用--SimpleAdapter使用详解(一)》
2、《List控件使用--SimpleAdapter使用详解(二)》
3、《PullToRefresh使用详解(一)--构建下拉刷新的ListView》
其它相关文章
4、《PullToRefresh使用详解(一)--构建下拉刷新的listView》
5、《PullToRefresh使用详解(三)--实现异步加载的下拉刷新列表》
6、《PullToRefresh使用详解(四)--利用回调函数实现到底加载》
7、《PullToRefresh使用详解(五)--下拉刷新的ScrollView》
效果图:
正在刷新 刷新后
一、XML代码
1、activity_main.xml
PullToRefresh标准写法,与《PullToRefresh使用详解(一)--构建下拉刷新的ListView》布局一样。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
-
-
- com.handmark.pulltorefresh.library.PullToRefreshListView
- android:id="@+id/pull_refresh_list"
- android:cacheColorHint="#00000000"
- android:divider="#19000000"
- android:dividerHeight="4dp"
- android:fadingEdge="none"
- android:fastScrollEnabled="false"
- android:footerDividersEnabled="false"
- android:headerDividersEnabled="false"
- android:smoothScrollbar="true"/>
-
- </LinearLayout>
2、数据项XML(item.xml)
与《List控件使用--SimpleAdapter使用详解(二)》布局一样,只是将名字改成了item.xml
android:orientation="horizontal"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- ImageViewandroid:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5px"/>
- LinearLayoutandroid:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"TextViewandroid:id="@+id/name"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFF00"
- android:textSize="22px"TextViewandroid:id="@+id/info"
- android:textColor="#FF00FFFF"
- android:textSize="13px"
二、JAVA代码
先贴出全部代码,然后再慢慢讲。
完整代码
packagecom.example.try_pulltorefresh_map;
-
-
- *完成了从TXT文本中提取,并向下刷新
- *blog:http://blog.csdn.net/harvic880925/article/details/17708409
- *@authorharvic
- *@date2013-12-31
- *
- */
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importjava.io.UnsupportedEncodingException;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importorg.json.JSONArray;
- importcom.handmark.pulltorefresh.library.PullToRefreshBase;
- importcom.handmark.pulltorefresh.library.PullToRefreshListView;
- importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
- importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
- importandroid.os.AsyncTask;
- importandroid.os.Bundle;
- importandroid.app.ListActivity;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.text.format.DateUtils;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.ImageView;
- importandroid.widget.ListView;
- importandroid.widget.TextView;
- publicclassMainActivityextendsListActivity{
- privateArrayList<HashMap<String,Object>>listItem=newArrayList<HashMap<String,Object>>();
- privatePullToRefreshListViewmPullRefreshListView;
- MyAdapteradapter=null;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mPullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);
- //设定下拉监听函数
- mPullRefreshListView.setOnRefreshListener(newOnRefreshListener<ListView>(){
- @Override
- voidonRefresh(PullToRefreshBase<ListView>refreshView){
- Stringlabel=DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),
- DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_ABBREV_ALL);
- //UpdatetheLastUpdatedLabel
- refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
- //Doworktorefreshthelisthere.
- newGetDataTask().execute();
- }
- });
- mPullRefreshListView.setMode(Mode.PULL_FROM_END);
- listItem=getData();
- adapter=newMyAdapter(this);
- //设置适配器
- ListViewactualListView=mPullRefreshListView.getRefreshableView();
- actualListView.setAdapter(adapter);
- privateclassGetDataTaskextendsAsyncTask<Void,Void,HashMap<String,Object>>{
- //后台处理部分
- protectedHashMap<String,Object>doInBackground(Void...params){
- //Simulatesabackgroundjob.
- try{
- Thread.sleep(1000);
- }catch(InterruptedExceptione){
- HashMap<String,Object>map=newHashMap<String,Object>();
- try{
- map= map.put("name","林珊");
- map.put("info","上传了一张新照片油画");
- map.put("img","youhua");
- catch(Exceptione){
- //TODO:handleexception
- setTitle("map出错了");
- returnnull;
- }
- returnmap;
- //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
- //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
- voidonPostExecute(HashMap<String,Object>result){
- //在头部增加新添内容
- listItem.add(result);
- //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
- adapter.notifyDataSetChanged();
- //CallonRefreshCompletewhenthelisthasbeenrefreshed.
- mPullRefreshListView.onRefreshComplete();
- setTitle(e.getMessage());
- super.onPostExecute(result);
- ArrayList<HashMap<String,Object>>list= InputStreaminputStream;
- inputStream=this.getAssets().open("my_home_friends.txt");
- Stringjson=readTextFile(inputStream);
- JSONArrayarray=newJSONArray(json);
- for(inti=0;i<array.length();i++){
- "name"));
- "info"));
- "photo"));
- list.add(map);
- returnlist;
- }catch(Exceptione){
- //TODO:handleexception
- e.printStackTrace();
- finalclassViewHolder{
- publicImageViewimg;
- publicTextViewname;
- publicTextViewinfo;
- classMyAdapterextendsBaseAdapter{
- privateLayoutInflatermInflater;
- publicMyAdapter(Contextcontext){
- this.mInflater=LayoutInflater.from(context);
- intgetCount(){
- //TODOAuto-generatedmethodstub
- returnlistItem.size();
- publicObjectgetItem(intarg0){
- longgetItemId(return0;
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- ViewHolderholder=if(convertView==null){
- holder=newViewHolder();
- convertView=mInflater.inflate(R.layout.item,null);
- holder.img=(ImageView)convertView.findViewById(R.id.img);
- holder.name=(TextView)convertView.findViewById(R.id.name);
- holder.info=(TextView)convertView.findViewById(R.id.info);
- convertView.setTag(holder);
- else{
- holder=(ViewHolder)convertView.getTag();
- holder.img.setImageBitmap(getHome((String)listItem.get(position).get("img")));
- holder.name.setText((String)listItem.get(position).get("name"));
- holder.info.setText((String)listItem.get(position).get("info"));
- returnconvertView;
- *根据图片名称获取主页图片
- publicBitmapgetHome(Stringphoto){
- StringhomeName=photo+".jpg";
- InputStreamis= is=getAssets().open("home/"+homeName);
- Bitmapbitmap=BitmapFactory.decodeStream(is);
- is.close();
- returnbitmap;
- ////工具类
- /**
- *
- *@paraminputStream
- *@return
- */
- publicStringreadTextFile(InputStreaminputStream){
- StringreadedStr="";
- BufferedReaderbr;
- br=newBufferedReader(newInputStreamReader(inputStream,"UTF-8"));
- Stringtmp;
- while((tmp=br.readLine())!=null){
- readedStr+=tmp;
- br.close();
- inputStream.close();
- catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- catch(IOExceptione){
- returnreadedStr;
- }
讲解:
执行流程: 看起来代码挺长,其实只看OnCreate()函数就能知道,只是分成了几大块,先贴出OnCreate()函数
}
1、首先初始化mPullRefreshListView,然后设定监听函数,监听函数没变,我只是更改了GetDataTask()函数里的部分代码,我相信大家也能看懂,难度不大;
2、设定下拉模式,绑定适配器,对应代码
mPullRefreshListView.setMode(Mode.PULL_FROM_END);
而这里的适配器是经过重写的,新生成了一个类MyAdapterextends BaseAdapter,关于重写适配器的方法,参考《List控件使用--SimpleAdapter使用详解(二)》;
到这就结束了,由于这篇文章是几篇文章的功能整合,很多东西相关的三篇文章都已经讲过了,也就没必要再讲,所以这里讲的比较粗略。
源码地址:http://download.csdn.net/detail/harvic880925/6790941(不要分,仅供分享)
请大家尊重原创者版权,转载请标明出处:http://www.52php.cn/article/p-eqxnrlgc-bae.html,谢谢! (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|