MainActivity中Xlistview实现下拉刷新和上拉加载

来源:互联网 发布:linux 断电 丢失 文件 编辑:程序博客网 时间:2024/06/05 02:47
package com.example.zhangjinling20170911;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.google.gson.Gson;import view.XListView;import view.XListView.IXListViewListener;import android.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity implements IXListViewListener{     public static final String   TAG="MainActivity";        int pageSize=3;        int page=1;        private List<Bean.DataBean> alllist = new ArrayList<Bean.DataBean>();        Bean datas;        List<Bean.DataBean> mlist;        XListView mxl;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          String  mpath="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize="+pageSize+"&"+"page="+page;            MyAsyncTask myAsyncTask = new MyAsyncTask(mpath);            myAsyncTask.execute();            //初始化组件            mxl= (XListView) findViewById(R.id.xl);            mxl.setPullLoadEnable(true);            mxl.setPullRefreshEnable(true);            mxl.setXListViewListener(this);        }        //下拉刷新        @Override        public void onRefresh() {            Log.i(TAG, "onRefresh: ");            page++;            //加载刷新数据            String  mpath="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize="+pageSize+"&"+"page="+page;            MyAsyncTask  mMyasync=new MyAsyncTask(mpath);            mMyasync.execute();            onLoad();        }        //上拉加载        @Override        public void onLoadMore() {            Log.i(TAG, "onLoadMore: ");            page++;            //加载刷新数据            String  mpath="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize="+pageSize+"&"+"page="+page;            MyAsyncTask  mMyasync=new MyAsyncTask(mpath);            mMyasync.execute();            onLoad();        }        private void onLoad() {            // TODO Auto-generated method stub            //停止刷新            mxl.stopRefresh();            //停止加载更多            mxl.stopLoadMore();            // 设置日期格式            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            // 获取当前系统时间            String nowTime = df.format(new Date(System.currentTimeMillis()));            // 释放时提示正在刷新时的当前时间            mxl.setRefreshTime(nowTime);        }        class   MyAsyncTask extends AsyncTask{            String mpath;            public MyAsyncTask(String mpath) {                super();                this.mpath=mpath;            }            @Override            protected Object doInBackground(Object[] objects) {                Https  mHttps=new Https();                String    json= mHttps.getJsonByInternet(mpath);                Log.i("MainActivity", "onCreate: "+json.toString());                Gson  mgjson=new Gson();                datas= mgjson.fromJson(json,Bean.class);                mlist= datas.getData();                return null;            }            @Override            protected void onPostExecute(Object o) {                super.onPostExecute(o);                //初始化适配器                alllist.addAll(mlist);                MyAdapter  myadapter=new MyAdapter(alllist,MainActivity.this);                mxl.setAdapter(myadapter);                myadapter.notifyDataSetChanged();            }        }    }


阅读全文
0 0
原创粉丝点击