PullToRefreshListView

做上拉刷新下来加载的,library包和gson

MainActivity

public class MainActivity extends AppCompatActivity {
    String url="http://169.254.167.244:8080/Web_Data/GetPhones?pagesize=20&pageindex=";
    int index=1;
    private PullToRefreshListView lv;
    List> list;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(PullToRefreshListView)findViewById(R.id.lv);
        list=new ArrayList<>();
        adapter=new MyAdapter(this,list);
        lv.setAdapter(adapter);
        getData();
        //下拉刷新的事件
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                index=1;//每次下拉刷新的时候,都请求第一页的数据
                list.clear();//把之前获取的数据全部清掉
                getData();
            }
        });

        //上拉加载新数据的事件
        lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                index++;//请求下一页
                getData();
            }
        });
    }

    public void getData(){
        new MyAsyncTask(new MyAsyncTask.CallBack() {
            @Override
            public void sendResult(String json) {
                Gson gson=new Gson();
                Phone phone = gson.fromJson(json, Phone.class);
                for (int i=0;i map=new HashMap();
                    map.put("id",String.valueOf(phone.getPhones().get(i).getId()));
                    map.put("brand",phone.getPhones().get(i).getBrand());
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
                lv.onRefreshComplete();//新数据加载完了,停止刷新
            }
        }).execute(url+index);
    }
}
适配器

public class MyAdapter extends BaseAdapter {
    Context context;
    List> list;
    public MyAdapter(Context context, List> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.list_item,null);
            vh=new ViewHolder();
            vh.tv_id=(TextView)convertView.findViewById(R.id.tv_id);
            vh.tv_brand=(TextView)convertView.findViewById(R.id.tv_brand);
            convertView.setTag(vh);
        }else {
            vh=(ViewHolder)convertView.getTag();
        }
        vh.tv_id.setText(list.get(position).get("id"));
        vh.tv_brand.setText(list.get(position).get("brand"));
        return convertView;
    }

    class ViewHolder{
        TextView tv_id;
        TextView tv_brand;
    }
}
异步任务加载网络下载数据

public class MyAsyncTask extends AsyncTask {
    //Context context;
    CallBack callBack;
    public MyAsyncTask(CallBack callBack){
        //this.context=context;
        this.callBack=callBack;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url=new URL(params[0]);
            HttpURLConnection huc=(HttpURLConnection) url.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode()==200){
                InputStream inputStream = huc.getInputStream();
                byte[] b=new byte[1024];
                int len=0;
                StringBuilder sb=new StringBuilder();
                while ((len=inputStream.read(b))!=-1){
                    sb.append(new String(b,0,len));
                }
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        callBack.sendResult(s);
    }

    interface CallBack{
        void sendResult(String json);
    }
}

实体类就不做了

MainActivity布局


    


list_item布局


    
    




你可能感兴趣的:(PullToRefreshListView)