OA系统信用盘新增三个极速彩版本Anroid中ListView实现分页加载

1.需要开启权限,AndroidManifest.xml文件中

OA系统信用盘新增三个极速彩版本  下载地址  QQ2952777280

  1.  
    <uses-permission android:name="android.permission.INTERNET"/>
  2.  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2.activity_main.xml文件中

  1.  
    <com.t20.weather.view.RefreshListView
  2.  
    android:id="@+id/lvWeather"
  3.  
    android:layout_width="match_parent"
  4.  
    android:layout_height="wrap_content"
  5.  
    android:layout_alignParentLeft="true"
  6.  
    android:layout_alignParentTop="true" >
  7.  
    </com.t20.weather.view.RefreshListView>

3.MainActivity.java文件中

  1.  
    package com.t20.weather;
  2.  
     
  3.  
    import java.util.List;
  4.  
     
  5.  
    import com.google.gson.Gson;
  6.  
    import com.google.gson.reflect.TypeToken;
  7.  
    import com.lidroid.xutils.BitmapUtils;
  8.  
    import com.lidroid.xutils.HttpUtils;
  9.  
    import com.lidroid.xutils.exception.HttpException;
  10.  
    import com.lidroid.xutils.http.ResponseInfo;
  11.  
    import com.lidroid.xutils.http.callback.RequestCallBack;
  12.  
    import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  13.  
    import com.t20.weather.entity.Weather;
  14.  
    import com.t20.weather.view.RefreshListView;
  15.  
    import com.t20.weather.view.RefreshListView.OnRefreshListener;
  16.  
     
  17.  
    import android.os.Bundle;
  18.  
    import android.util.Log;
  19.  
    import android.view.View;
  20.  
    import android.view.ViewGroup;
  21.  
    import android.widget.BaseAdapter;
  22.  
    import android.widget.ImageView;
  23.  
    import android.widget.TextView;
  24.  
    import android.widget.Toast;
  25.  
    import android.app.Activity;
  26.  
     
  27.  
    public class MainActivity extends Activity {
  28.  
    private List<Weather> weatherList;
  29.  
    private RefreshListView lvWeather;// 升级后的ListView控件,在RefreshListView.java中进行升级强化
  30.  
    private MyAdapator myAdapator;// 自定义适配器
  31.  
    private Integer pageIndex = 1;
  32.  
     
  33.  
    @Override
  34.  
    protected void onCreate(Bundle savedInstanceState) {
  35.  
    super.onCreate(savedInstanceState);
  36.  
    setContentView(R.layout.activity_main);
  37.  
     
  38.  
    // 获得控件
  39.  
    lvWeather = (RefreshListView) findViewById(R.id.lvWeather);
  40.  
     
  41.  
    final HttpUtils hu = new HttpUtils();
  42.  
    //xxx.xxx.xxx.xxx代表要连接的ip地址
  43.  
    hu.send(HttpMethod.GET,
  44.  
    "http://xxx.xxx.xxx.xxx:8088/WeatherHT/WeatherServlet?pageIndex="
  45.  
    + pageIndex, new RequestCallBack<String>() {
  46.  
     
  47.  
    /**
  48.  
    * 成功
  49.  
    */
  50.  
    @Override
  51.  
    public void onSuccess(ResponseInfo<String> responseInfo) {
  52.  
    // 获得返回的结果
  53.  
    String ret = responseInfo.result;
  54.  
     
  55.  
    // 利用Gson解析数据
  56.  
    Gson gson = new Gson();
  57.  
    weatherList = gson.fromJson(ret,
  58.  
    new TypeToken<List<Weather>>() {
  59.  
    }.getType());
  60.  
     
  61.  
    // 自定义适配器
  62.  
    myAdapator = new MyAdapator();
  63.  
    lvWeather.setAdapter(myAdapator);
  64.  
     
  65.  
    // 显示下一页数据
  66.  
    pageIndex++;
  67.  
     
  68.  
    lvWeather.setOnRefreshListener( new OnRefreshListener() {
  69.  
     
  70.  
    @Override
  71.  
    public void refresh() {
  72.  
    //xxx.xxx.xxx.xxx代表要连接的ip地址
  73.  
    hu.send(HttpMethod.GET,
  74.  
    "http://xxx.xxx.xxx.xxx:8088/WeatherHT/WeatherServlet?pageIndex="
  75.  
    + pageIndex,
  76.  
    new RequestCallBack<String>() {
  77.  
     
  78.  
    @Override
  79.  
    public void onSuccess(
  80.  
    ResponseInfo<String> responseInfo) {
  81.  
    // TODO Auto-generated method
  82.  
    // stub
  83.  
    // 获取到剩下的数据
  84.  
    String ret = responseInfo.result;
  85.  
     
  86.  
    // 数据全部显示完毕后给出提示
  87.  
    if (ret.equals("no")) {
  88.  
    Toast.makeText(
  89.  
    MainActivity. this,
  90.  
    "信息已经全部加载完成!",
  91.  
    Toast.LENGTH_SHORT)
  92.  
    .show();
  93.  
    } else {
  94.  
    List<Weather> wList = new Gson()
  95.  
    .fromJson(
  96.  
    ret,
  97.  
    new TypeToken<List<Weather>>() {
  98.  
    }.getType());
  99.  
     
  100.  
    // 将剩下的数据全部放入到wList集合中
  101.  
    weatherList.addAll(wList);
  102.  
     
  103.  
    // 通知ListView进行内容刷新
  104.  
    myAdapator
  105.  
    .notifyDataSetChanged();
  106.  
     
  107.  
    // 显示完一页后再准备显示下一页
  108.  
    pageIndex++;
  109.  
    }
  110.  
     
  111.  
    // 数据全部加载完毕,关闭网络请求,隐藏底部加载部分(refresh_foot_view.xml的样式)
  112.  
    lvWeather.loadFinish();
  113.  
     
  114.  
    }
  115.  
     
  116.  
    @Override
  117.  
    public void onFailure(
  118.  
    HttpException error,
  119.  
    String msg) {
  120.  
     
  121.  
    // 数据全部加载完毕,关闭网络请求,隐藏底部加载部分(refresh_foot_view.xml的样式)
  122.  
    lvWeather.loadFinish();
  123.  
    }
  124.  
     
  125.  
    });
  126.  
     
  127.  
    }
  128.  
    });
  129.  
    }
  130.  
     
  131.  
    /**
  132.  
    * 错误,异常
  133.  
    */
  134.  
    @Override
  135.  
    public void onFailure(HttpException error, String msg) {
  136.  
    // TODO Auto-generated method stub
  137.  
    Log.e( "错误信息", msg);
  138.  
    }
  139.  
    });
  140.  
    }
  141.  
     
  142.  
    /**
  143.  
    * 自定义适配器
  144.  
    *
  145.  
    * @author Administrator
  146.  
    *
  147.  
    */
  148.  
    class MyAdapator extends BaseAdapter {
  149.  
     
  150.  
    @Override
  151.  
    public int getCount() {
  152.  
    // TODO Auto-generated method stub
  153.  
    return weatherList.size();// 数量
  154.  
    }
  155.  
     
  156.  
    @Override
  157.  
    public Object getItem(int position) {
  158.  
    // TODO Auto-generated method stub
  159.  
    return null;
  160.  
    }
  161.  
     
  162.  
    @Override
  163.  
    public long getItemId(int position) {
  164.  
    // TODO Auto-generated method stub
  165.  
    return 0;
  166.  
    }
  167.  
     
  168.  
    @Override
  169.  
    public View getView(int position, View v, ViewGroup parent) {
  170.  
    // 将list_view布局放到ListView中
  171.  
    v = View.inflate(MainActivity. this, R.layout.list_item, null);
  172.  
    // 获取要显示数据的控件,控件在list_view中
  173.  
    TextView tvCity = (TextView) v.findViewById(R.id.tvCity);
  174.  
    TextView tvType = (TextView) v.findViewById(R.id.tvType);
  175.  
    TextView tvTemp = (TextView) v.findViewById(R.id.tvTemp);
  176.  
    ImageView ivImg = (ImageView) v.findViewById(R.id.ivImg);
  177.  
     
  178.  
    // 显示相应的数据
  179.  
    tvCity.setText(weatherList.get(position).getCity());
  180.  
    tvType.setText(weatherList.get(position).getType());
  181.  
    tvTemp.setText(weatherList.get(position).getTemp() + "");
  182.  
     
  183.  
    // 显示图片
  184.  
    BitmapUtils bitmapUtils = new BitmapUtils(MainActivity.this);
  185.  
    bitmapUtils.display(ivImg, weatherList.get(position).getImg());
  186.  
     
  187.  
    return v;
  188.  
    }
  189.  
     
  190.  
    }
  191.  
    }
4.list_item.xml文件中
  1.  
    <ImageView
  2.  
    android:id="@+id/ivImg"
  3.  
    android:layout_width="wrap_content"
  4.  
    android:layout_height="wrap_content"
  5.  
    android:src="@drawable/ic_launcher"
  6.  
    android:layout_weight="1"/>
  7.  
     
  8.  
    <TextView
  9.  
    android:id="@+id/tvCity"
  10.  
    android:layout_width="wrap_content"
  11.  
    android:layout_height="wrap_content"
  12.  
    android:layout_marginLeft="15dp"
  13.  
    android:layout_marginTop="14dp"
  14.  
    android:text="城市"
  15.  
    android:layout_weight="1"/>
  16.  
     
  17.  
    <TextView
  18.  
    android:id="@+id/tvType"
  19.  
    android:layout_width="wrap_content"
  20.  
    android:layout_height="wrap_content"
  21.  
    android:layout_marginLeft="15dp"
  22.  
    android:layout_marginTop="14dp"
  23.  
    android:text="天气类型"
  24.  
    android:layout_weight="1" />
  25.  
     
  26.  
    <TextView
  27.  
    android:id="@+id/tvTemp"
  28.  
    android:layout_width="wrap_content"
  29.  
    android:layout_height="wrap_content"
  30.  
    android:layout_marginLeft="15dp"
  31.  
    android:layout_marginTop="14dp"
  32.  
    android:text="温度"
  33.  
    android:layout_weight="1"/>

5.refresh_foot_view.xml文件中

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:layout_width="match_parent"
  4.  
    android:layout_height="match_parent" >
  5.  
     
  6.  
    <RelativeLayout
  7.  
    android:layout_width="match_parent"
  8.  
    android:layout_height="match_parent" >
  9.  
     
  10.  
    <TextView
  11.  
    android:id="@+id/textView1"
  12.  
    android:layout_width="wrap_content"
  13.  
    android:layout_height="wrap_content"
  14.  
    android:layout_alignParentRight="true"
  15.  
    android:layout_below="@+id/progressBar1"
  16.  
    android:layout_marginRight="76dp"
  17.  
    android:layout_marginTop="17dp"
  18.  
    android:text="玩命加载中..."
  19.  
    android:textAppearance="?android:attr/textAppearanceMedium" />
  20.  
     
  21.  
    <ProgressBar
  22.  
    android:id="@+id/progressBar1"
  23.  
    android:layout_width="wrap_content"
  24.  
    android:layout_height="wrap_content"
  25.  
    android:layout_alignLeft="@+id/textView1"
  26.  
    android:layout_alignParentTop="true"
  27.  
    android:layout_marginLeft="20dp" />
  28.  
    </RelativeLayout>
  29.  
     
  30.  
    </LinearLayout>

6.RefreshListView.java文件中

  1.  
    package com.t20.weather.view;
  2.  
     
  3.  
    import com.t20.weather.R;
  4.  
     
  5.  
    import android.content.Context;
  6.  
    import android.util.AttributeSet;
  7.  
    import android.view.View;
  8.  
    import android.widget.AbsListView;
  9.  
    import android.widget.ListView;
  10.  
     
  11.  
    public class RefreshListView extends ListView {
  12.  
    private View footView;
  13.  
    private int mHeight;// 测量的底部高度
  14.  
    private boolean loadFlag = false;// 判断是否正在加载,true表示正在加载
  15.  
     
  16.  
    public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
  17.  
    super(context, attrs, defStyle);
  18.  
    // TODO Auto-generated constructor stub
  19.  
    initRefreshListView();
  20.  
    }
  21.  
     
  22.  
    public RefreshListView(Context context, AttributeSet attrs) {
  23.  
    super(context, attrs);
  24.  
    // TODO Auto-generated constructor stub
  25.  
    initRefreshListView();
  26.  
    }
  27.  
     
  28.  
    public RefreshListView(Context context) {
  29.  
    super(context);
  30.  
    // TODO Auto-generated constructor stub
  31.  
    initRefreshListView();
  32.  
    }
  33.  
     
  34.  
    /**
  35.  
    * 初始化方法
  36.  
    */
  37.  
    public void initRefreshListView() {
  38.  
    footView = View.inflate(getContext(), R.layout.refresh_foot_view, null);
  39.  
     
  40.  
    // 将布局追加到ListView的脚部(底部)
  41.  
    addFooterView(footView);
  42.  
     
  43.  
    // 量布局的高度,0的意思是从0刻度开始量
  44.  
    // 低版本有一个严重的Bug,不支持相对布局,但是在Level17以后得到修复,可以利用线性布局包住相对布局
  45.  
    footView.measure( 0, 0);
  46.  
     
  47.  
    // 量到高度
  48.  
    mHeight = footView.getMeasuredHeight();
  49.  
     
  50.  
    // 利用内边距实现隐藏加载部分,若要显示则将-mHeight改成0
  51.  
    footView.setPadding( 0, -mHeight, 0, 0);
  52.  
     
  53.  
    // 设置监听
  54.  
    this.setOnScrollListener(new OnScrollListener() {
  55.  
     
  56.  
    /**
  57.  
    * 滚动状态改变(由滚动到静止或由静止到滚动)
  58.  
    */
  59.  
    @Override
  60.  
    public void onScrollStateChanged(AbsListView view, int scrollState) {
  61.  
    // TODO Auto-generated method stub
  62.  
     
  63.  
    }
  64.  
     
  65.  
    /**
  66.  
    * 滚动过程中
  67.  
    */
  68.  
    @Override
  69.  
    public void onScroll(AbsListView view, int firstVisibleItem,
  70.  
    int visibleItemCount, int totalItemCount) {
  71.  
    // 监听是否滚动到最后一刻,getCount() - 1表示已经滚动到最后
  72.  
    //滚动到最后时,开始加载
  73.  
    if (getLastVisiblePosition() == getCount() - 1) {
  74.  
    // 显示底部加载部分
  75.  
    footView.setPadding( 0, 0, 0, 0);
  76.  
     
  77.  
    // 在准备进行加载时,如果没有在进行加载并且接口对象不为空时,才进行数据刷新,此时数据正在加载,加载状态变为true
  78.  
    if (loadFlag == false) {
  79.  
    if (onRefreshListener != null) {
  80.  
    onRefreshListener.refresh();
  81.  
    loadFlag = true;
  82.  
    }
  83.  
    }
  84.  
    }
  85.  
     
  86.  
    }
  87.  
     
  88.  
    });
  89.  
     
  90.  
    }
  91.  
     
  92.  
    /**
  93.  
    * 定义网络连接接口,让外部调用
  94.  
    *
  95.  
    * @author Administrator
  96.  
    *
  97.  
    */
  98.  
    public interface OnRefreshListener {
  99.  
    // 定义刷新数据方法,在MainActivity.java文件中刷新加载下一页数据时调用并实现
  100.  
    void refresh();
  101.  
    }
  102.  
     
  103.  
    // 接口对象
  104.  
    private OnRefreshListener onRefreshListener;
  105.  
     
  106.  
    /*
  107.  
    * 给接口对象设置setter和getter方便外部调用
  108.  
    */
  109.  
    public OnRefreshListener getOnRefreshListener() {
  110.  
    return onRefreshListener;
  111.  
    }
  112.  
     
  113.  
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
  114.  
    this.onRefreshListener = onRefreshListener;
  115.  
    }
  116.  
     
  117.  
    /**
  118.  
    * 数据全部加载完毕后,隐藏底部加载部分
  119.  
    */
  120.  
    public void loadFinish() {
  121.  
    footView.setPadding( 0, -mHeight, 0, 0);
  122.  
    // 完成加载,改变加载状态
  123.  
    loadFlag = false;
  124.  
    }
  125.  
     
  126.  
    }

猜你喜欢

转载自www.cnblogs.com/woQQ1159880099/p/9267963.html