ZMY_分页加载
来源:互联网 发布:iphone铃声设置mac 编辑:程序博客网 时间:2024/05/13 12:54
//分页加载
package com.zmy.a6_;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.gson.Gson;
import com.zmy.a6_.Weixin.Details;
public class MainActivity extends Activity {
private MyAdapter adapter;
private ListView listView;
private ProgressBar pb;
private String path;
private int position=0;
private ArrayList<Details> list;
private ArrayList<Details> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<Weixin.Details>();
listView = (ListView) findViewById(R.id.listView);
// 执行异步任务
lodedata();
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
switch (scrollState) {
//停止滑动
case OnScrollListener.SCROLL_STATE_IDLE:
// 判断一下当前的最后一个条目是不是集合的长度减一
int lastVisiblePosition = view.getLastVisiblePosition();
if(lastVisiblePosition==arrayList.size()-1){
position++;
lodedata();
}
break;
default:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
private void lodedata() {
// TODO Auto-generated method stub
path = "http://v.juhe.cn/weixin/query";
MyAyscntask myAyscntask=new MyAyscntask();
myAyscntask.execute();
}
// 第一次进来就加载判断listView有没有值--同时显示进度条
class MyAyscntask extends AsyncTask<Void, Void, String>{
private ProgressDialog progressDialog;
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
//客户端请求
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(path);
List<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("key","0917c812f187b92e025c9b7c2d4e59b5" ));
//页面每次加1
parameters.add(new BasicNameValuePair("pno", (position+1)+""));
HttpEntity entity=new UrlEncodedFormEntity(parameters);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==200){
InputStream inputStream = httpResponse.getEntity().getContent();
String str = Streamtostr(inputStream);
return str;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Gson gson=new Gson();
Weixin wx = gson.fromJson(result, Weixin.class);
list = wx.result.list;
//提前创建一个ArrayList<Details> 集合 将list存放
arrayList.addAll(list);
// 消失进度条
progressDialog.dismiss();
if(adapter==null){
adapter = new MyAdapter();
listView.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
//消失进度条
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正努力加载中...");
progressDialog.show();
}
}
private String Streamtostr(InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
arrayOutputStream.write(buffer, 0, len);
}
return arrayOutputStream.toString();
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView= View.inflate(MainActivity.this, R.layout.list_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.lv_imageView = (ImageView) convertView
.findViewById(R.id.lv_imageView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(arrayList.get(position).title);
final String path = arrayList.get(position).firstImg;
if(TextUtils.isEmpty(path)){
holder.lv_imageView.setImageResource(R.drawable.ic_launcher);
}else{
holder.lv_imageView.setTag(path);
new Thread(){
public void run() {
data(path);
};
}.start();
}
return convertView;
}
}
class ViewHolder{
TextView textView;
ImageView lv_imageView;
}
private void data(String path) {
// TODO Auto-generated method stub
try {
URL url=new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setReadTimeout(5000);
openConnection.setConnectTimeout(5000);
int responseCode = openConnection.getResponseCode();
if(responseCode==200){
InputStream inputStream = openConnection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
Message msg=Message.obtain();
msg.obj=bitmap;
Bundle data=new Bundle();
data.putString("path", path);
msg.setData(data);
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Handler handler=new Handler(){
@SuppressLint("NewApi")
public void handleMessage(android.os.Message msg) {
Bitmap bitmap=(Bitmap) msg.obj;
Bundle data = msg.getData();
String path = data.getString("path", null);
ImageView img = (ImageView) listView.findViewWithTag(path);
if(bitmap!=null&&img!=null){
img.setImageBitmap(bitmap);
}
};
};
}
0 0
- ZMY_分页加载
- ZMY_自定义分页加载
- ZMY_补间动画
- ZMY_工厂模式
- 分页加载
- 分页加载
- 分页加载
- 分页加载
- ZMY_异步任务抽象类
- ZMY_二级列表适配
- ZMY_动态添加图片文字
- ListView分页加载,加载更多
- ListView分页加载
- listview分页加载
- ios UITableView分页加载
- Android_listview分页加载更多
- listview分页加载问题
- ListView的分页加载
- 正则表达式
- 字符串函数小结---老是忘记
- hadoop生态系统学习之路(三)java实现上传文件(本地或ftp)至hdfs
- 一种编码而已
- ViewPagerIndicator
- ZMY_分页加载
- 基于UDP/IP 协议的Socket程序
- Listener监听器
- 初学visual studio 2013 遇到的几个问题
- MySQL学习(二)
- 2016 [Offer收割]编程练习赛3 A B
- shell命令二
- mybatis第4天
- 性能调优可以从哪些方面着手