1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android列表分页功能的实现 往下拉时刷新数据(显示正在加载中)

Android列表分页功能的实现 往下拉时刷新数据(显示正在加载中)

时间:2022-05-22 09:31:13

相关推荐

Android列表分页功能的实现 往下拉时刷新数据(显示正在加载中)

1、需要在AndroidManifest.xml清单文件中获取InterNet权限

2、重写ListView控件(创建RefreshListView.java,继承自ListView)

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 int mHeight;private View footview;public RefreshListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub//加载初始化布局intiRefreshListView();}public RefreshListView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub//加载初始化布局intiRefreshListView();}public RefreshListView(Context context) {super(context);// TODO Auto-generated constructor stub//加载初始化布局intiRefreshListView();}/*** 初始化布局*/public void intiRefreshListView(){//加载布局(用于显示加载的图标 )footview=View.inflate(getContext(),R.layout.refresh_foot_view, null);//将布局追加到ListView底部addFooterView(footview);//用尺子量高度(低版本安卓有严重的Bug,不支持相对布局,在Level 17版本后得到修复)footview.measure(0, 0);//-------宽偏移量---高偏移量//获取测量过后的高度mHeight=footview.getMeasuredHeight();//通过设置内边距,隐藏refresh_foot_view.xml布局footview.setPadding(0, -mHeight, 0, 0);//设置监听ListView的滚动事件this.setOnScrollListener(new OnScrollListener() {/*** 滚动状态改变*/@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {// TODO Auto-generated method stub}/*** 滚动中*/@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// TODO Auto-generated method stub//当ListView滚动到最底部时if(getLastVisiblePosition()==getCount()-1){//让refresh_foot_view.xml布局显示出来(显示加载中)footview.setPadding(0, 0, 0, 0);//判断是否在请求网络数据 if(loadingFlag==false){if(onRefreshListener!=null){onRefreshListener.RefeshData();loadingFlag=true;//正在请求}}}}});}//是否正在请求网络,防止出现重复请求的情况(true表示正在请求,false不请求)private boolean loadingFlag=false;/*** 数据加载完毕要执行的方法*/public void loadFinish(){//通过设置内边距,隐藏refresh_foot_view.xml布局footview.setPadding(0, -mHeight, 0, 0);//请求完毕loadingFlag=false;}private OnRefreshListener onRefreshListener;/*** @param onRefreshListener the onRefreshListener to set*/public void setOnRefreshListener(OnRefreshListener onRefreshListener) {this.onRefreshListener = onRefreshListener;}/*** 自定义一个接口,用来刷新网络数据* @author Administrator**/public interface OnRefreshListener{void RefeshData();}}

3、创建天气实体类Weather.java(主要用于封装服务端获取到的天气信息)

package com.t20.weather.entity;import java.io.Serializable;/*** 天气实体类* @author admin**/public class Weather implements Serializable {private Integer id;//城市编码private String city;//城市名private String tianqi;//天气private double temp;//温度private String img;//天气图标public Weather() {super();}public Weather(Integer id, String city, String tianqi, double temp,String img) {super();this.id = id;this.city = city;this.tianqi = tianqi;this.temp = temp;this.img = img;}/*** @return the id*/public Integer getId() {return id;}/*** @param id the id to set*/public void setId(Integer id) {this.id = id;}/*** @return the city*/public String getCity() {return city;}/*** @param city the city to set*/public void setCity(String city) {this.city = city;}/*** @return the tianqi*/public String getTianqi() {return tianqi;}/*** @param tianqi the tianqi to set*/public void setTianqi(String tianqi) {this.tianqi = tianqi;}/*** @return the temp*/public double getTemp() {return temp;}/*** @param temp the temp to set*/public void setTemp(double temp) {this.temp = temp;}/*** @return the img*/public String getImg() {return img;}/*** @param img the img to set*/public void setImg(String img) {this.img = img;}}

4、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.app.Activity;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;public class MainActivity extends Activity {private RefreshListView lvWeather;private List<Weather> weaList;private MyAdapter myAdapter;private int pageIndex=1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lvWeather=(RefreshListView) findViewById(R.id.lvWeather);//使用xUtils框架的HttpUtils加载后台数据到列表集合里final HttpUtils hu=new HttpUtils();String url="http://172.168.40.97:8088/android0/WeatherServlet?pageIndex="+pageIndex;hu.send(HttpMethod.GET, url, new RequestCallBack<String>(){@Overridepublic void onFailure(HttpException error, String msg) {// TODO Auto-generated method stubLog.e("xUtils错误信息", msg);}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {// TODO Auto-generated method stubString json=responseInfo.result;//使用gson读取json中的内容Gson gson=new Gson();weaList=gson.fromJson(json, new TypeToken<List<Weather>>(){}.getType());//设置适配器myAdapter=new MyAdapter();lvWeather.setAdapter(myAdapter);pageIndex++;//监听RefreshListView控件lvWeather.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void RefeshData() {// TODO Auto-generated method stub//使用xUtils框架的HttpUtils加载后台数据到列表集合里String url="http://172.168.40.97:8088/android0/WeatherServlet?pageIndex="+pageIndex;hu.send(HttpMethod.GET, url, new RequestCallBack<String>(){@Overridepublic void onFailure(HttpException error,String msg) {// TODO Auto-generated method stubLog.e("xUtils错误信息", msg);Toast.makeText(MainActivity.this, "网络异常",Toast.LENGTH_SHORT).show();lvWeather.loadFinish();}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {// TODO Auto-generated method stubString ret=responseInfo.result;//先判断是否还有数据if(ret.equals("no")){Toast.makeText(MainActivity.this, "没有更多数据了",Toast.LENGTH_SHORT).show();}else{//使用gson读取json中的内容Gson gson=new Gson();//用临时集合接收数据 List<Weather> tempList=gson.fromJson(ret, new TypeToken<List<Weather>>(){}.getType());//把临时集合的数据加到天气集合中weaList.addAll(tempList);//让适配器通知ListView刷新数据myAdapter.notifyDataSetChanged();pageIndex++;}lvWeather.loadFinish();}});}});}});}class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn weaList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View view, ViewGroup parent) {// TODO Auto-generated method stub//1.加载list_item.xml布局文件view=View.inflate(MainActivity.this, R.layout.list_item, null);//------------------上下文-------------需要加载的布局----一般用null//2.获取list_item.xml布局文件中的控件/*TextView tvId=(TextView) view.findViewById(R.id.tvId);*/TextView tvCity=(TextView) view.findViewById(R.id.tvCity);TextView tvTianqi=(TextView) view.findViewById(R.id.tvTianqi);TextView tvTemp=(TextView) view.findViewById(R.id.tvTemp);ImageView ivImg=(ImageView) view.findViewById(R.id.ivImg);//3.设置控件值Weather weather=weaList.get(position);/*tvId.setText(weather.getId()+"");*/tvCity.setText(weather.getCity());tvTianqi.setText(weather.getTianqi());tvTemp.setText(weather.getTemp()+"");//显示网络图片BitmapUtils bu=new BitmapUtils(MainActivity.this);String uri=weather.getImg();bu.display(ivImg, uri);return view;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。