正文  UI设计 > ListView >

Android ListView下拉刷新

Android ListView下拉刷新[代码] [Java]代码package com.stay.main; 002 003 import java.net.HttpURLConnection; 004 import java.util.ArrayList; 005 import java.util.HashMap; 006 007 imp......

Android ListView下拉刷新
[代码] [Java]代码
 
package com.stay.main;  
 
002  
 
003 import java.net.HttpURLConnection;  
 
004 import java.util.ArrayList;  
 
005 import java.util.HashMap;  
 
006  
 
007 import org.json.JSONObject;  
 
008  
 
009 import android.app.Activity;  
 
010 import android.app.ProgressDialog;  
 
011 import android.graphics.drawable.Drawable;  
 
012 import android.os.Bundle;  
 
013 import android.os.Handler;  
 
014 import android.os.HandlerThread;  
 
015 import android.os.Looper;  
 
016 import android.os.Message;  
 
017 import android.view.LayoutInflater;  
 
018 import android.view.View;  
 
019 import android.widget.AbsListView;  
 
020 import android.widget.AbsListView.OnScrollListener;  
 
021 import android.widget.Button;  
 
022 import android.widget.LinearLayout;  
 
023 import android.widget.ListView;  
 
024 import android.widget.RelativeLayout;  
 
025 import android.widget.TextView;  
 
026  
 
027 import com.stay.wifi.R;  
 
028  
 
029 /**  
 
030 * @author Stay  
 
031 * 动态加载listview数据,上拉 刷新,下拉 更多  
 
032 */ 
 
033 public class ListViewActivity extends Activity implements OnScrollListener {  
 
034 private static final int LOAD = 0;  
 
035 private static final int ERROR = 0;  
 
036 private static final int MEMBER = 1;  
 
037 private static final int LOADED = 2;  
 
038 private static final int DIALOG = 3;  
 
039 private static final int FULL = 4;  
 
040 private NearbyAdapter adapter;  
 
041 private ListView nearby_lv;  
 
042 private RelativeLayout nearby_lv_header;  
 
043 private Button list_bottom_btn;  
 
044 private LinearLayout list_bottom_linear;  
 
045 private TextView bottom_progress_text;  
 
046 private RelativeLayout nearby_lv_footer;  
 
047 private Button list_header_btn;  
 
048 private LinearLayout list_header_linear;  
 
049 private TextView heard_progress_text;  
 
050 private ArrayList<JSONObject> nearby_data = new ArrayList<JSONObject>();  
 
051 private int lastItem;  
 
052 private HashMap<String, Drawable> imageCache;  
 
053 private com.stay.main.ListViewActivity.MyHandler myHandler;  
 
054 private ProgressDialog dialog;  
 
055 private int curPage = 1;  
 
056 private boolean isMember = false;  
 
057 private int firstItem;  
 
058 public int count;  
 
059  
 
060 /** Called when the activity is first created. */ 
 
061 @Override 
 
062 public void onCreate(Bundle savedInstanceState) {  
 
063 super.onCreate(savedInstanceState);  
 
064 setContentView(R.layout.main);  
 
065 initView();  
 
066 myHandler.sendEmptyMessage(LOAD);  
 
067 }  
 
068  
 
069 @Override 
 
070 public void onScrollStateChanged(AbsListView view, int scrollState) {  
 
071 DebugUtil.debug("onScrollStateChanged");  
 
072 //当滚动停止且滚动的总数等于数据的总数,去加载  
 
073 if (lastItem == count && scrollState == SCROLL_STATE_IDLE) {  
 
074 DebugUtil.debug("onScrollStateChanged--------next");  
 
075 if (curPage == 4 && !isMember) {  
 
076 DebugUtil.show(this, "您不是正式会员,请申请正式会员,");  
 
077 list_bottom_linear.setVisibility(View.GONE);  
 
078 } else {  
 
079 //加载数据  
 
080 myHandler.sendEmptyMessage(LOAD);  
 
081 }  
 
082 return;  
 
083 }  
 
084 //当往上拉时更新数据,将data清空然后去重新加载  
 
085 if (firstItem == 0 && scrollState == SCROLL_STATE_IDLE) {  
 
086 DebugUtil.debug("onScrollStateChanged--------refresh");  
 
087 curPage = 0;  
 
088 myHandler.sendEmptyMessage(LOAD);  
 
089 }  
 
090 }  
 
091  
 
092 @Override 
 
093 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
 
094 DebugUtil.debug("firstVisibleItem=" + firstVisibleItem);  
 
095 DebugUtil.debug("visibleItemCount=" + visibleItemCount);  
 
096 DebugUtil.debug("totalItemCount=" + totalItemCount);  
 
097 //这里要减二,因为我加了header footer  
 
098 lastItem = firstVisibleItem + visibleItemCount - 2;  
 
099 firstItem = firstVisibleItem;  
 
100 }  
 
101  
 
102 public int getData() {  
 
103 try {  
 
104 HttpURLConnection conn = DownloadUtil.download("http://zdevl.mapi.jjdd.com/nearby/lbs?page=" + curPage);  
 
105 ArrayList<JSONObject> temp = JSONUtil.streamToJsonList(conn.getInputStream());  
 
106 if (curPage == 0 && nearby_data.size() > 0) {  
 
107 nearby_data.clear();  
 
108 count = 0;  
 
109 }  
 
110 if (temp != null && temp.size() > 0) {  
 
111 count += temp.size();  
 
112 nearby_data.addAll(temp);  
 
113 DebugUtil.debug("nearby_data.size()="+nearby_data.size());  
 
114 } else {  
 
115 return FULL;  
 
116 }  
 
117 return LOADED;  
 
118 } catch (Exception e) {  
 
119 return ERROR;  
 
120 }  
 
121 }  
 
122  
 
123 private Handler handler = new Handler() {  
 
124 @Override 
 
125 public void handleMessage(Message msg) {  
 
126 super.handleMessage(msg);  
 
127 switch (msg.what) {  
 
128 case DIALOG:  
 
129 list_bottom_linear.setVisibility(View.VISIBLE);  
 
130 list_header_linear.setVisibility(View.VISIBLE);  
 
131 break;  
 
132 case LOADED:  
 
133 list_bottom_linear.setVisibility(View.GONE);  
 
134 list_header_linear.setVisibility(View.GONE);  
 
135 curPage++;  
 
136 adapter.notifyDataSetChanged();  
 
137 break;  
 
138 case ERROR:  
 
139 DebugUtil.debug("error,missing data");  
 
140 break;  
 
141 case MEMBER:  
 
142 DebugUtil.debug("you must regist formal member");  
 
143 break;  
 
144 default:  
 
145 break;  
 
146 }  
 
147 }  
 
148 };  
 
149 //创建子线程加载数据,然后更新  
 
150 private class MyHandler extends Handler {  
 
151 private int status;  
 
152  
 
153 public MyHandler(Looper looper) {  
 
154 super(looper);  
 
155 }  
 
156  
 
157 @Override 
 
158 public void handleMessage(Message msg) {  
 
159 synchronized (this) {  
 
160 switch (msg.what) {  
 
161 case LOAD:// get data from server  
 
162 handler.sendEmptyMessage(DIALOG);//显示等待框  
 
163 status = getData();  
 
164 handler.sendEmptyMessageDelayed(status, 1000);  
 
165 break;  
 
166 default:  
 
167 break;  
 
168 }  
 
169 }  
 
170 }  
 
171 }  
 
172  
 
173 public void initView() {  
 
174 imageCache = new HashMap<String, Drawable>();  
 
175 HandlerThread handlerThread = new HandlerThread("nearby");  
 
176 // 在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();  
 
177 handlerThread.start();  
 
178 myHandler = new MyHandler(handlerThread.getLooper());  
 
179 nearby_lv = (ListView) findViewById(R.id.nearby_lv);  
 
180 nearby_lv_footer = (RelativeLayout) LayoutInflater.from(ListViewActivity.this).inflate(R.layout.nearby_lv_header, null);  
 
181 list_bottom_btn = (Button) nearby_lv_footer.findViewById(R.id.list_bottom_btn);  
 
182 list_bottom_linear = (LinearLayout) nearby_lv_footer.findViewById(R.id.list_bottom_linear);  
 
183 bottom_progress_text = (TextView) nearby_lv_footer.findViewById(R.id.progress_text);  
 
184 nearby_lv_header = (RelativeLayout) LayoutInflater.from(ListViewActivity.this).inflate(R.layout.nearby_lv_header, null);  
 
185 list_header_btn = (Button) nearby_lv_header.findViewById(R.id.list_bottom_btn);  
 
186 list_header_linear = (LinearLayout) nearby_lv_header.findViewById(R.id.list_bottom_linear);  
 
187 heard_progress_text = (TextView) nearby_lv_header.findViewById(R.id.progress_text);  
 
188 list_header_btn.setText("刷新");  
 
189 list_bottom_btn.setText("更多");  
 
190 list_header_linear.setVisibility(View.GONE);  
 
191 nearby_lv.addHeaderView(nearby_lv_header);  
 
192 nearby_lv.addFooterView(nearby_lv_footer);  
 
193 // list_header_btn.setOnClickListener(header_click);  
 
194 adapter = new NearbyAdapter(ListViewActivity.this, nearby_data);  
 
195 nearby_lv.setAdapter(adapter);  
 
196 nearby_lv.setOnScrollListener(ListViewActivity.this);  
 
197 }  
 
198 } 
 
[代码] [Java]代码
 
package com.stay.main;  
 
002  
 
003 import java.net.HttpURLConnection;  
 
004 import java.util.ArrayList;  
 
005 import java.util.HashMap;  
 
006  
 
007 import org.json.JSONObject;  
 
008  
 
009 import android.app.Activity;  
 
010 import android.app.ProgressDialog;  
 
011 import android.graphics.drawable.Drawable;  
 
012 import android.os.Bundle;  
 
013 import android.os.Handler;  
 
014 import android.os.HandlerThread;  
 
015 import android.os.Looper;  
 
016 import android.os.Message;  
 
017 import android.view.LayoutInflater;  
 
018 import android.view.View;  
 
019 import android.widget.AbsListView;  
 
020 import android.widget.AbsListView.OnScrollListener;  
 
021 import android.widget.Button;  
 
022 import android.widget.LinearLayout;  
 
023 import android.widget.ListView;  
 
024 import android.widget.RelativeLayout;  
 
025 import android.widget.TextView;  
 
026  
 
027 import com.stay.wifi.R;  
 
028  
 
029 /**  
 
030 * @author Stay  
 
031 * 动态加载listview数据,上拉 刷新,下拉 更多  
 
032 */ 
 
033 public class ListViewActivity extends Activity implements OnScrollListener {  
 
034 private static final int LOAD = 0;  
 
035 private static final int ERROR = 0;  
 
036 private static final int MEMBER = 1;  
 
037 private static final int LOADED = 2;  
 
038 private static final int DIALOG = 3;  
 
039 private static final int FULL = 4;  
 
040 private NearbyAdapter adapter;  
 
041 private ListView nearby_lv;  
 
042 private RelativeLayout nearby_lv_header;  
 
043 private Button list_bottom_btn;  
 
044 private LinearLayout list_bottom_linear;  
 
045 private TextView bottom_progress_text;  
 
046 private RelativeLayout nearby_lv_footer;  
 
047 private Button list_header_btn;  
 
048 private LinearLayout list_header_linear;  
 
049 private TextView heard_progress_text;  
 
050 private ArrayList<JSONObject> nearby_data = new ArrayList<JSONObject>();  
 
051 private int lastItem;  
 
052 private HashMap<String, Drawable> imageCache;  
 
053 private com.stay.main.ListViewActivity.MyHandler myHandler;  
 
054 private ProgressDialog dialog;  
 
055 private int curPage = 1;  
 
056 private boolean isMember = false;  
 
057 private int firstItem;  
 
058 public int count;  
 
059  
 
060 /** Called when the activity is first created. */ 
 
061 @Override 
 
062 public void onCreate(Bundle savedInstanceState) {  
 
063 super.onCreate(savedInstanceState);  
 
064 setContentView(R.layout.main);  
 
065 initView();  
 
066 myHandler.sendEmptyMessage(LOAD);  
 
067 }  
 
068  
 
069 @Override 
 
070 public void onScrollStateChanged(AbsListView view, int scrollState) {  
 
071 DebugUtil.debug("onScrollStateChanged");  
 
072 //当滚动停止且滚动的总数等于数据的总数,去加载  
 
073 if (lastItem == count && scrollState == SCROLL_STATE_IDLE) {  
 
074 DebugUtil.debug("onScrollStateChanged--------next");  
 
075 if (curPage == 4 && !isMember) {  
 
076 DebugUtil.show(this, "您不是正式会员,请申请正式会员,");  
 
077 list_bottom_linear.setVisibility(View.GONE);  
 
078 } else {  
 
079 //加载数据  
 
080 myHandler.sendEmptyMessage(LOAD);  
 
081 }  
 
082 return;  
 
083 }  
 
084 //当往上拉时更新数据,将data清空然后去重新加载  
 
085 if (firstItem == 0 && scrollState == SCROLL_STATE_IDLE) {  
 
086 DebugUtil.debug("onScrollStateChanged--------refresh");  
 
087 curPage = 0;  
 
088 myHandler.sendEmptyMessage(LOAD);  
 
089 }  
 
090 }  
 
091  
 
092 @Override 
 
093 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
 
094 DebugUtil.debug("firstVisibleItem=" + firstVisibleItem);  
 
095 DebugUtil.debug("visibleItemCount=" + visibleItemCount);  
 
096 DebugUtil.debug("totalItemCount=" + totalItemCount);  
 
097 //这里要减二,因为我加了header footer  
 
098 lastItem = firstVisibleItem + visibleItemCount - 2;  
 
099 firstItem = firstVisibleItem;  
 
100 }  
 
101  
 
102 public int getData() {  
 
103 try {  
 
104 HttpURLConnection conn = DownloadUtil.download("http://zdevl.mapi.jjdd.com/nearby/lbs?page=" + curPage);  
 
105 ArrayList<JSONObject> temp = JSONUtil.streamToJsonList(conn.getInputStream());  
 
106 if (curPage == 0 && nearby_data.size() > 0) {  
 
107 nearby_data.clear();  
 
108 count = 0;  
 
109 }  
 
110 if (temp != null && temp.size() > 0) {  
 
111 count += temp.size();  
 
112 nearby_data.addAll(temp);  
 
113 DebugUtil.debug("nearby_data.size()="+nearby_data.size());  
 
114 } else {  
 
115 return FULL;  
 
116 }  
 
117 return LOADED;  
 
118 } catch (Exception e) {  
 
119 return ERROR;  
 
120 }  
 
121 }  
 
122  
 
123 private Handler handler = new Handler() {  
 
124 @Override 
 
125 public void handleMessage(Message msg) {  
 
126 super.handleMessage(msg);  
 
127 switch (msg.what) {  
 
128 case DIALOG:  
 
129 list_bottom_linear.setVisibility(View.VISIBLE);  
 
130 list_header_linear.setVisibility(View.VISIBLE);  
 
131 break;  
 
132 case LOADED:  
 
133 list_bottom_linear.setVisibility(View.GONE);  
 
134 list_header_linear.setVisibility(View.GONE);  
 
135 curPage++;  
 
136 adapter.notifyDataSetChanged();  
 
137 break;  
 
138 case ERROR:  
 
139 DebugUtil.debug("error,missing data");  
 
140 break;  
 
141 case MEMBER:  
 
142 DebugUtil.debug("you must regist formal member");  
 
143 break;  
 
144 default:  
 
145 break;  
 
146 }  
 
147 }  
 
148 };  
 
149 //创建子线程加载数据,然后更新  
 
150 private class MyHandler extends Handler {  
 
151 private int status;  
 
152  
 
153 public MyHandler(Looper looper) {  
 
154 super(looper);  
 
155 }  
 
156  
 
157 @Override 
 
158 public void handleMessage(Message msg) {  
 
159 synchronized (this) {  
 
160 switch (msg.what) {  
 
161 case LOAD:// get data from server  
 
162 handler.sendEmptyMessage(DIALOG);//显示等待框  
 
163 status = getData();  
 
164 handler.sendEmptyMessageDelayed(status, 1000);  
 
165 break;  
 
166 default:  
 
167 break;  
 
168 }  
 
169 }  
 
170 }  
 
171 }  
 
172  
 
173 public void initView() {  
 
174 imageCache = new HashMap<String, Drawable>();  
 
175 HandlerThread handlerThread = new HandlerThread("nearby");  
 
176 // 在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();  
 
177 handlerThread.start();  
 
178 myHandler = new MyHandler(handlerThread.getLooper());  
 
179 nearby_lv = (ListView) findViewById(R.id.nearby_lv);  
 
180 nearby_lv_footer = (RelativeLayout) LayoutInflater.from(ListViewActivity.this).inflate(R.layout.nearby_lv_header, null);  
 
181 list_bottom_btn = (Button) nearby_lv_footer.findViewById(R.id.list_bottom_btn);  
 
182 list_bottom_linear = (LinearLayout) nearby_lv_footer.findViewById(R.id.list_bottom_linear);  
 
183 bottom_progress_text = (TextView) nearby_lv_footer.findViewById(R.id.progress_text);  
 
184 nearby_lv_header = (RelativeLayout) LayoutInflater.from(ListViewActivity.this).inflate(R.layout.nearby_lv_header, null);  
 
185 list_header_btn = (Button) nearby_lv_header.findViewById(R.id.list_bottom_btn);  
 
186 list_header_linear = (LinearLayout) nearby_lv_header.findViewById(R.id.list_bottom_linear);  
 
187 heard_progress_text = (TextView) nearby_lv_header.findViewById(R.id.progress_text);  
 
188 list_header_btn.setText("刷新");  
 
189 list_bottom_btn.setText("更多");  
 
190 list_header_linear.setVisibility(View.GONE);  
 
191 nearby_lv.addHeaderView(nearby_lv_header);  
 
192 nearby_lv.addFooterView(nearby_lv_footer);  
 
193 // list_header_btn.setOnClickListener(header_click);  
 
194 adapter = new NearbyAdapter(ListViewActivity.this, nearby_data);  
 
195 nearby_lv.setAdapter(adapter);  
 
196 nearby_lv.setOnScrollListener(ListViewActivity.this);  
 
197 }  
 
198 }