刷新

来源:互联网 发布:有关网络暴力的电影 编辑:程序博客网 时间:2024/04/29 18:50

public class MainActivity extends Activity implements IXListViewListener {


    String refresh = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&min_behot_time=1465266200&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1465265107&loc_mode=5&lac=4527&cid=28883&iid=4464422609&device_id=17258390367&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=SAMSUNG&os_api=19&os_version=4.4.4&uuid=353040417908000&openudid=84c1c7b192991cc6";


    String more="http://ic.snssdk.com/2/article/v25/stream/?category=news_hot&count=20&max_behot_time=1465886546&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1465889498&loc_mode=5&lac=4527&cid=28883&iid=4464422609&device_id=17258390367&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=SAMSUNG&os_api=19&os_version=4.4.4&uuid=353040417908000&openudid=84c1c7b192991cc6";


    LinkedList<DataBean> list = new LinkedList<DataBean>();

    Handler handler = new Handler() {
        private MyAdapter myAdapter;

        @SuppressWarnings("unchecked")
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
            // 加载初始化数据
            case 1:
                list = (LinkedList<DataBean>) msg.obj;
                myAdapter = new MyAdapter(MainActivity.this, list);

                xlistView.setAdapter(myAdapter);
                // loadOver();
                break;
            // 刷新
            case 2:
                list.clear();
                for (DataBean dataBean : (LinkedList<DataBean>)msg.obj) {
                    //象开头添加
                    list.addFirst(dataBean);
                }
                // 刷新适配器
                myAdapter.notifyDataSetChanged();
                loadOver();
                break;
            // 加载更多
            case 3:

                for (DataBean dataBean : (LinkedList<DataBean>)msg.obj) {
                    //象结尾添加
                    list.addLast(dataBean);
                }
                // 刷新适配器
                myAdapter.notifyDataSetChanged();
                loadOver();
                break;
            }

        };

    };

    private XListView xlistView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        xlistView = (XListView) findViewById(R.id.xlistView);

        // 下拉刷新是否激活
        xlistView.setPullRefreshEnable(true);
        // 加载更多是否激活
        xlistView.setPullLoadEnable(true);
        // xlistView刷新和加载更多的监听
        xlistView.setXListViewListener(this);

        getData(1,refresh);


    }

    public void getData(final int i,String path) {

        HttpUtils httpUtils = new HttpUtils();
        
        httpUtils.configCurrentHttpCacheExpiry(0);
        
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {

                String result = arg0.result;

                Gson gson = new Gson();

                Data data = gson.fromJson(result, Data.class);
                Message msg = Message.obtain();
                msg.obj = data.getData();
                msg.what = i;
                handler.sendMessage(msg);
            }
        });

    }

    // 停止加载更多/刷新
    public void loadOver() {
        // 停止加载更多
        xlistView.stopLoadMore();
        // 停止刷新
        xlistView.stopRefresh();
        //设置刷新时的时间
        xlistView.setRefreshTime(getTime());
        
        
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {

        getData(2,refresh);

    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {

        getData(3,more);
    }

public String getTime(){
    
    
    SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
    
    Date date=new Date();
    String time = format.format(date);
    return time;
    
    
}

}

0 0
原创粉丝点击