BaseActivity+OkHttp+xlistview+分页加载

package bwie.zhengaihua20161107;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ArrayAdapter;

import com.google.gson.Gson;

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

import baseactivity.BaseActivity;
import butterknife.Bind;
import xlistview.XListView;

public class Main2Activity extends BaseActivity implements XListView.IXListViewListener {
    private ArrayAdapter<Bean.ResultBean.DataBean> adapter;
    private int tag;
    private int index = 1;
    @Bind(R.id.lv)
    XListView lv;

    private String url = "http://japi.juhe.cn/joke/content/list.from?key=%20874ed931559ba07aade103eee279bb37%20&pagesize=10&sort=asc&time=1418745237&page=";
    List<Bean.ResultBean.DataBean> newsList = new ArrayList<Bean.ResultBean.DataBean>();
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String result = (String) msg.obj;
            tag = msg.arg1;
            Gson gson = new Gson();
            Bean be = gson.fromJson(result, Bean.class);
            List<Bean.ResultBean.DataBean> list = be.getResult().getData();
            switch (tag) {
                case 1:
                    newsList.addAll(list);
                    adapter = new ArrayAdapter<Bean.ResultBean.DataBean>(Main2Activity.this, android.R.layout.simple_list_item_1, newsList);
                    lv.setAdapter(adapter);
                    break;
                case 2:
                    newsList.clear();
                    newsList.addAll(list);
                    adapter.notifyDataSetChanged();
                    lv.stopRefresh();
                    break;

                case 3:
                    newsList.addAll(list);
                    adapter.notifyDataSetChanged();
                    lv.stopLoadMore();
                    break;
            }
            super.handleMessage(msg);
        }
    };


    /**
     * @param url
     * @param tag
     */

    private void getEnqueue(String url, final int tag) {

        getAsyn(url, tag, handler);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main2;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        lv.setPullLoadEnable(true);
        lv.setXListViewListener(this);
    }

    @Override
    public void loadData() {
        getEnqueue(url + (index++), 1);
    }

    @Override
    public void onRefresh() {
        index = 1;
        getEnqueue(url + (index++), 2);

    }

    @Override
    public void onLoadMore() {
        getEnqueue(url + (index++), 3);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值