1.需要开启权限,AndroidManifest.xml文件中
OA系统信用盘新增三个极速彩版本 下载地址 QQ2952777280
-
<uses-permission android:name="android.permission.INTERNET"/>
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.activity_main.xml文件中
-
<com.t20.weather.view.RefreshListView
-
android:id="@+id/lvWeather"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentLeft="true"
-
android:layout_alignParentTop="true" >
-
</com.t20.weather.view.RefreshListView>
3.MainActivity.java文件中
-
package com.t20.weather;
-
import java.util.List;
-
import com.google.gson.Gson;
-
import com.google.gson.reflect.TypeToken;
-
import com.lidroid.xutils.BitmapUtils;
-
import com.lidroid.xutils.HttpUtils;
-
import com.lidroid.xutils.exception.HttpException;
-
import com.lidroid.xutils.http.ResponseInfo;
-
import com.lidroid.xutils.http.callback.RequestCallBack;
-
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
-
import com.t20.weather.entity.Weather;
-
import com.t20.weather.view.RefreshListView;
-
import com.t20.weather.view.RefreshListView.OnRefreshListener;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.BaseAdapter;
-
import android.widget.ImageView;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
import android.app.Activity;
-
public class MainActivity extends Activity {
-
private List<Weather> weatherList;
-
private RefreshListView lvWeather;// 升级后的ListView控件,在RefreshListView.java中进行升级强化
-
private MyAdapator myAdapator;// 自定义适配器
-
private Integer pageIndex = 1;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
// 获得控件
-
lvWeather = (RefreshListView) findViewById(R.id.lvWeather);
-
final HttpUtils hu = new HttpUtils();
-
//xxx.xxx.xxx.xxx代表要连接的ip地址
-
hu.send(HttpMethod.GET,
-
"http://xxx.xxx.xxx.xxx:8088/WeatherHT/WeatherServlet?pageIndex="
-
+ pageIndex, new RequestCallBack<String>() {
-
/**
-
* 成功
-
*/
-
@Override
-
public void onSuccess(ResponseInfo<String> responseInfo) {
-
// 获得返回的结果
-
String ret = responseInfo.result;
-
// 利用Gson解析数据
-
Gson gson = new Gson();
-
weatherList = gson.fromJson(ret,
-
new TypeToken<List<Weather>>() {
-
}.getType());
-
// 自定义适配器
-
myAdapator = new MyAdapator();
-
lvWeather.setAdapter(myAdapator);
-
// 显示下一页数据
-
pageIndex++;
-
lvWeather.setOnRefreshListener(new OnRefreshListener() {
-
@Override
-
public void refresh() {
-
//xxx.xxx.xxx.xxx代表要连接的ip地址
-
hu.send(HttpMethod.GET,
-
"http://xxx.xxx.xxx.xxx:8088/WeatherHT/WeatherServlet?pageIndex="
-
+ pageIndex,
-
new RequestCallBack<String>() {
-
@Override
-
public void onSuccess(
-
ResponseInfo<String> responseInfo) {
-
// TODO Auto-generated method
-
// stub
-
// 获取到剩下的数据
-
String ret = responseInfo.result;
-
// 数据全部显示完毕后给出提示
-
if (ret.equals("no")) {
-
Toast.makeText(
-
MainActivity.this,
-
"信息已经全部加载完成!",
-
Toast.LENGTH_SHORT)
-
.show();
-
} else {
-
List<Weather> wList = new Gson()
-
.fromJson(
-
ret,
-
new TypeToken<List<Weather>>() {
-
}.getType());
-
// 将剩下的数据全部放入到wList集合中
-
weatherList.addAll(wList);
-
// 通知ListView进行内容刷新
-
myAdapator
-
.notifyDataSetChanged();
-
// 显示完一页后再准备显示下一页
-
pageIndex++;
-
}
-
// 数据全部加载完毕,关闭网络请求,隐藏底部加载部分(refresh_foot_view.xml的样式)
-
lvWeather.loadFinish();
-
}
-
@Override
-
public void onFailure(
-
HttpException error,
-
String msg) {
-
// 数据全部加载完毕,关闭网络请求,隐藏底部加载部分(refresh_foot_view.xml的样式)
-
lvWeather.loadFinish();
-
}
-
});
-
}
-
});
-
}
-
/**
-
* 错误,异常
-
*/
-
@Override
-
public void onFailure(HttpException error, String msg) {
-
// TODO Auto-generated method stub
-
Log.e("错误信息", msg);
-
}
-
});
-
}
-
/**
-
* 自定义适配器
-
*
-
* @author Administrator
-
*
-
*/
-
class MyAdapator extends BaseAdapter {
-
@Override
-
public int getCount() {
-
// TODO Auto-generated method stub
-
return weatherList.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 v, ViewGroup parent) {
-
// 将list_view布局放到ListView中
-
v = View.inflate(MainActivity.this, R.layout.list_item, null);
-
// 获取要显示数据的控件,控件在list_view中
-
TextView tvCity = (TextView) v.findViewById(R.id.tvCity);
-
TextView tvType = (TextView) v.findViewById(R.id.tvType);
-
TextView tvTemp = (TextView) v.findViewById(R.id.tvTemp);
-
ImageView ivImg = (ImageView) v.findViewById(R.id.ivImg);
-
// 显示相应的数据
-
tvCity.setText(weatherList.get(position).getCity());
-
tvType.setText(weatherList.get(position).getType());
-
tvTemp.setText(weatherList.get(position).getTemp() + "");
-
// 显示图片
-
BitmapUtils bitmapUtils = new BitmapUtils(MainActivity.this);
-
bitmapUtils.display(ivImg, weatherList.get(position).getImg());
-
return v;
-
}
-
}
-
}
4.list_item.xml文件中
-
<ImageView
-
android:id="@+id/ivImg"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:src="@drawable/ic_launcher"
-
android:layout_weight="1"/>
-
<TextView
-
android:id="@+id/tvCity"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginLeft="15dp"
-
android:layout_marginTop="14dp"
-
android:text="城市"
-
android:layout_weight="1"/>
-
<TextView
-
android:id="@+id/tvType"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginLeft="15dp"
-
android:layout_marginTop="14dp"
-
android:text="天气类型"
-
android:layout_weight="1" />
-
<TextView
-
android:id="@+id/tvTemp"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginLeft="15dp"
-
android:layout_marginTop="14dp"
-
android:text="温度"
-
android:layout_weight="1"/>
5.refresh_foot_view.xml文件中
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
<RelativeLayout
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
<TextView
-
android:id="@+id/textView1"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentRight="true"
-
android:layout_below="@+id/progressBar1"
-
android:layout_marginRight="76dp"
-
android:layout_marginTop="17dp"
-
android:text="玩命加载中..."
-
android:textAppearance="?android:attr/textAppearanceMedium" />
-
<ProgressBar
-
android:id="@+id/progressBar1"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignLeft="@+id/textView1"
-
android:layout_alignParentTop="true"
-
android:layout_marginLeft="20dp" />
-
</RelativeLayout>
-
</LinearLayout>
6.RefreshListView.java文件中
-
package com.t20.weather.view;
-
import com.t20.weather.R;
-
import android.content.Context;
-
import android.util.AttributeSet;
-
import android.view.View;
-
import android.widget.AbsListView;
-
import android.widget.ListView;
-
public class RefreshListView extends ListView {
-
private View footView;
-
private int mHeight;// 测量的底部高度
-
private boolean loadFlag = false;// 判断是否正在加载,true表示正在加载
-
public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
-
super(context, attrs, defStyle);
-
// TODO Auto-generated constructor stub
-
initRefreshListView();
-
}
-
public RefreshListView(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
// TODO Auto-generated constructor stub
-
initRefreshListView();
-
}
-
public RefreshListView(Context context) {
-
super(context);
-
// TODO Auto-generated constructor stub
-
initRefreshListView();
-
}
-
/**
-
* 初始化方法
-
*/
-
public void initRefreshListView() {
-
footView = View.inflate(getContext(), R.layout.refresh_foot_view, null);
-
// 将布局追加到ListView的脚部(底部)
-
addFooterView(footView);
-
// 量布局的高度,0的意思是从0刻度开始量
-
// 低版本有一个严重的Bug,不支持相对布局,但是在Level17以后得到修复,可以利用线性布局包住相对布局
-
footView.measure(0, 0);
-
// 量到高度
-
mHeight = footView.getMeasuredHeight();
-
// 利用内边距实现隐藏加载部分,若要显示则将-mHeight改成0
-
footView.setPadding(0, -mHeight, 0, 0);
-
// 设置监听
-
this.setOnScrollListener(new OnScrollListener() {
-
/**
-
* 滚动状态改变(由滚动到静止或由静止到滚动)
-
*/
-
@Override
-
public void onScrollStateChanged(AbsListView view, int scrollState) {
-
// TODO Auto-generated method stub
-
}
-
/**
-
* 滚动过程中
-
*/
-
@Override
-
public void onScroll(AbsListView view, int firstVisibleItem,
-
int visibleItemCount, int totalItemCount) {
-
// 监听是否滚动到最后一刻,getCount() - 1表示已经滚动到最后
-
//滚动到最后时,开始加载
-
if (getLastVisiblePosition() == getCount() - 1) {
-
// 显示底部加载部分
-
footView.setPadding(0, 0, 0, 0);
-
// 在准备进行加载时,如果没有在进行加载并且接口对象不为空时,才进行数据刷新,此时数据正在加载,加载状态变为true
-
if (loadFlag == false) {
-
if (onRefreshListener != null) {
-
onRefreshListener.refresh();
-
loadFlag = true;
-
}
-
}
-
}
-
}
-
});
-
}
-
/**
-
* 定义网络连接接口,让外部调用
-
*
-
* @author Administrator
-
*
-
*/
-
public interface OnRefreshListener {
-
// 定义刷新数据方法,在MainActivity.java文件中刷新加载下一页数据时调用并实现
-
void refresh();
-
}
-
// 接口对象
-
private OnRefreshListener onRefreshListener;
-
/*
-
* 给接口对象设置setter和getter方便外部调用
-
*/
-
public OnRefreshListener getOnRefreshListener() {
-
return onRefreshListener;
-
}
-
public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
-
this.onRefreshListener = onRefreshListener;
-
}
-
/**
-
* 数据全部加载完毕后,隐藏底部加载部分
-
*/
-
public void loadFinish() {
-
footView.setPadding(0, -mHeight, 0, 0);
-
// 完成加载,改变加载状态
-
loadFlag = false;
-
}
-
}
原文地址:https://www.cnblogs.com/woQQ1159880099/p/9267963.html