网络请求分页加载

package com.dqf;


import java.util.ArrayList;
import java.util.List;


import com.dqf.bean.Bean;
import com.dqf.bean.MyListAdapter;
import com.dqf.bean.Bean.DataEntity;
import com.google.gson.Gson;
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 android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;


public class MainActivity extends Activity implements OnScrollListener{


private boolean isLoad = false;
private ListView lv;
private MyListAdapter adapter;
int count = 1;
List<DataEntity> list=new ArrayList<DataEntity>();
List<DataEntity> data=new ArrayList<DataEntity>();
public Handler handler=new Handler(){



public void handleMessage(android.os.Message msg) {
int shu=msg.what;
switch (shu) {
case 0:
adapter = new MyListAdapter(list,MainActivity.this);
lv.setAdapter(adapter);
break;


default:
break;
}
};
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inidshuju();
        lv = (ListView)findViewById(R.id.lv);
initData("");
lv.setOnScrollListener(this);//设置滚动监听
    }


private void inidshuju() {
HttpUtils httpUtils=new HttpUtils();
httpUtils.send(HttpMethod.GET, "http://m.yunifang.com/yunifang/mobile/goods/getall?random=87749&encode=ac6bd45b8f50b626a6843b294af8fed5", new RequestCallBack<String>() {


@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub

}


@Override
public void onSuccess(ResponseInfo<String> arg0) {
String json = arg0.result;
System.out.println(json+"111");
Gson gson=new Gson();
Bean bean = gson.fromJson(json, Bean.class);
list.addAll(bean.getData());
handler.sendEmptyMessage(0);

}
});

}


private void initData(String string) {


data.addAll(list);
adapter = new MyListAdapter(data,MainActivity.this);
lv.setAdapter(adapter);
}


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
isLoad=((firstVisibleItem+visibleItemCount)==totalItemCount);

}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(isLoad&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
//Toast.makeText(getApplicationContext(), "开始加载数据", 1).show();
initData("");
adapter.notifyDataSetChanged();
}

}




  
    

}


布局代码



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
         />


</RelativeLayout>

<?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="wrap_content"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv1"
        />


</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值