`
zhengaihua
  • 浏览: 21531 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

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);
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics