PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx

上传人:b****5 文档编号:7161495 上传时间:2023-01-21 格式:DOCX 页数:19 大小:143.47KB
下载 相关 举报
PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx_第1页
第1页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx_第2页
第2页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx_第3页
第3页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx_第4页
第4页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx

《PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx》由会员分享,可在线阅读,更多相关《PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx(19页珍藏版)》请在冰豆网上搜索。

PullToRefresh使用详解实现异步加载的下拉刷新列表资料.docx

PullToRefresh使用详解实现异步加载的下拉刷新列表资料

PullToRefresh使用详解

(二)--实现异步加载的下拉刷新列表

前言:

根据前几篇的阶段性成果(下拉刷新、异步加载),将其集成,就成了这篇文章,这篇文章代码量比较大,对于异步加载的部分,除了更改了getView()里绑定部分的代码,其它的都没有动,所以异步刷新里的代码我就不往里贴了,只贴MainActivity.java的代码,主要看看主程序是如何实现异步加载图片和下拉刷新的。

效果图:

   初始化后,正在加载图片           加载出一部分

   

 

         下拉刷新                                    新生成的ITEM                              加载完成新生成ITEM的图片

   

   

 一、MainActivity.java

 其它的代码就不讲了,我只说说这个主页面是如何动作的,先看看整体代码。

 

[java]viewplaincopyprint?

1.package com.example.try_simpleadapter_new;  

2./** 

3. * 完成与服务器通信的下拉刷新 

4. * @author harvic 

5. */  

6.import java.io.BufferedReader;  

7.import java.io.InputStreamReader;  

8.import java.util.ArrayList;  

9.import java.util.List;  

10.  

11.import org.apache.http.HttpEntity;  

12.import org.apache.http.HttpResponse;  

13.import org.apache.http.NameValuePair;  

14.import org.apache.http.client.entity.UrlEncodedFormEntity;  

15.import org.apache.http.client.methods.HttpPost;  

16.import org.apache.http.impl.client.DefaultHttpClient;  

17.import org.apache.http.message.BasicNameValuePair;  

18.import org.apache.http.protocol.HTTP;  

19.import org.json.JSONArray;  

20.  

21.  

22.import com.handmark.pulltorefresh.library.PullToRefreshBase;  

23.import com.handmark.pulltorefresh.library.PullToRefreshListView;  

24.import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  

25.import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;  

26.  

27.import android.os.AsyncTask;  

28.import android.os.Bundle;  

29.import android.text.format.DateUtils;  

30.import android.util.Log;  

31.import android.widget.ListView;  

32.import android.app.ListActivity;  

33.  

34.public class MainActivity extends ListActivity{  

35.  

36.    private String serverIP="http:

//222.195.151.19";  

37.    private List mData;  

38.    private PullToRefreshListView mPullRefreshListView;  

39.    ImageAndTextListAdapter adapter=null;  

40.    @Override  

41.    public void onCreate(Bundle savedInstanceState) {  

42.        super.onCreate(savedInstanceState);  

43.        setContentView(R.layout.activity_main);   

44.          

45.        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);  

46.  

47.        //设定下拉监听函数  

48.        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {  

49.            @Override  

50.            public void onRefresh(PullToRefreshBase refreshView) {  

51.                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  

52.                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  

53.  

54.                // Update the LastUpdatedLabel  

55.                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  

56.  

57.                Log.d("msg","this=="+this);  

58.                // Do work to refresh the list here.  

59.                new GetDataTask().execute();  

60.                  

61.            }  

62.        });  

63.  

64.        mPullRefreshListView.setMode(Mode.PULL_FROM_END);// 设置底部下拉刷新模式  

65.        //传参生成适配器  

66.        mData = getData();  

67.        ListView actualListView = mPullRefreshListView.getRefreshableView();  

68.        adapter = new ImageAndTextListAdapter(this,mData,actualListView);  

69.          

70.        // 设置适配器  

71.        actualListView.setAdapter(adapter);       

72.    }  

73.      

74.    private List getData() {  

75.        //创建默认的httpClient实例.    

76.                DefaultHttpClient httpclient = new DefaultHttpClient();   

77.                HttpResponse response = null;  

78.                HttpEntity entity = null;  

79.                  

80.                StringBuilder builder = new StringBuilder();    

81.                JSONArray jsonArray = null;    

82.                  

83.                List list = new ArrayList();                  

84.                                                                       

85.                try{  

86.                     // 创建httpost.访问本地服务器网址    

87.                    HttpPost httpost = new HttpPost(serverIP+"/try_an_server/index.php");     

88.                      

89.                     //构造POST方法的{name:

value} 参数对  

90.                     List   vps = new ArrayList ();     

91.                     //将参数传入post方法中  

92.                     vps.add(new BasicNameValuePair("action", "insert"));    

93.                     vps.add(new BasicNameValuePair("name", "进去了"));  

94.                                   

95.                    httpost.setEntity(new UrlEncodedFormEntity(vps, HTTP.UTF_8));  

96.                    response = httpclient.execute(httpost);  //执行  

97.                      

98.                    if (response.getEntity() !

= null) {               

99.                        //如果服务器端JSON没写对,这句是会出异常,是执行不过去的  

100.                     BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));    

101.                     String s = reader.readLine();  

102.                     for (; s !

= null; s = reader.readLine()) {   

103.                         builder.append(s);    

104.                     }    

105.                     Log.i("msg","builder.toString = "+ builder.toString());   

106.                       

107.                     jsonArray = new JSONArray(builder.toString());    

108.                     for (int i = 0; i < jsonArray.length(); i++) {  

109.                         if(jsonArray.getJSONObject(i).getInt("id")==1){  

110.                             String name=jsonArray.getJSONObject(i).getString("name");  

111.                             String info=jsonArray.getJSONObject(i).getString("info");  

112.                             String PicName=jsonArray.getJSONObject(i).getString("photo");  

113.                             String picURL=serverIP+"/try_an_server/"+PicName+".jpg";  

114.                               

115.                            ImageAndText item=new ImageAndText(picURL,name,info);  

116.                            list.add(item);  

117.                         }  

118.                     }  

119.                    }  

120.                } catch (Exception e) {  

121.                    e.printStackTrace();  

122.                } finally {  

123.                    try {  

124.                         if (entity !

= null)  

125.                         {  

126.                            httpclient.getConnectionManager().shutdown();//关闭连接  

127.                            //这两种释放连接的方法都可以  

128.                         }  

129.                    } catch (Exception e) {  

130.                        // TODO Auto-generated catch block  

131.                        e.printStackTrace();  

132.                    }    

133.                }  

134.  

135.            return list;      

136.      

137.    }  

138.      

139.      

140.      

141.    private class GetDataTask extends AsyncTask {  

142.  

143.        //后台处理部分  

144.        @Override  

145.        protected ImageAndText doInBackground(Void... params) {  

146.            // Simulates a background job.  

147.            ImageAndText item = null;  

148.            try {  

149.                item = new ImageAndText(serverIP+"/try_an_server/xizang.jpg", "sss", "ssss");                 

150.            } catch (Exception e) {  

151.                // TODO:

 handle exception  

152.                setTitle("map出错了");  

153.            }  

154.              

155.            return item;  

156.        }  

157.  

158.        //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中  

159.        //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值  

160.        @Override  

161.        protected void onPostExecute(ImageAndText result) {  

162.            //在头部增加新添内容  

163.              

164.            try {  

165.                mData.add(result);  

166.                  

167.                //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合  

168.                adapter.notifyDataSetChanged();  

169.                adapter.loadImage();  

170.                // Call onRefreshComplete when the list has been refreshed.  

171.                mPullRefreshListView.onRefreshComplete();  

172.            } catch (Exception e) {  

173.                // TODO:

 handle exception  

174.                setTitle(e.getMessage());  

175.            }  

176.  

177.            super.onPostExecute(result);  

178.        }  

179.    }  

180.      

181.  

182.}  

packagecom.example.try_simpleadapter_new;

/**

*完成与服务器通信的下拉刷新

*@authorharvic

*/

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.List;

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.apache.http.NameValuePair;

importorg.apache.http.client.entity.UrlEncodedFormEntity;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.message.BasicNameValuePair;

importorg.apache.http.protocol.HTTP;

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.text.format.D

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1