Xlistview上拉刷新和下拉加载

package lianxi.bawei.com.day10_xlistview_lianxi;


import android.os.AsyncTask;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


import com.google.gson.Gson;


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


import lianxi.bawei.com.day10_xlistview_lianxi.adapter.MyAdapter;
import lianxi.bawei.com.day10_xlistview_lianxi.bean.SuperClass;
import lianxi.bawei.com.day10_xlistview_lianxi.utils.NetWorkUtils;


public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
    private  String JSON_URL="http://www.93.gov.cn/93app/data.do?channelId=2&startNum=1";
    int pageNum=1;
    private XListView mXlv;
    private  List list;
    private MyAdapter adapter;
    private DrawerLayout dl;//侧滑新添
    private ListView listviewdetail;//侧滑新添
    private ArrayList lists=new ArrayList();//侧滑新添
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mXlv = (XListView)findViewById(R.id.xlv);
        listviewdetail=(ListView)findViewById(R.id.listviewdetail);//侧滑新添
        dl=(DrawerLayout)findViewById(R.id.dl);//侧滑新添
        mXlv.setPullLoadEnable(true);
        mXlv.setXListViewListener(this);


        //侧滑新添
        listviewdetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                dl.closeDrawer(listviewdetail);
            }
        });
        Datas();//侧滑新添






        new AsyncTask(){


            @Override
            protected String doInBackground(String... strings) {
                NetWorkUtils netWorkUtils = new NetWorkUtils();
                String jsonStr = netWorkUtils.getJsonByHttpurlConnection(JSON_URL+pageNum);
                return jsonStr;
            }


            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                SuperClass fromJson = gson.fromJson(s, SuperClass.class);
                list = fromJson.getData();


                //创建适配器
                adapter=new MyAdapter(list,MainActivity.this);
                mXlv.setAdapter(adapter);


            }
        }.execute();


    }
    //侧滑新添
    private void Datas() {
//        for (int i=0;i<10;i++){
//            lists.add("杜晓彤"+i);
//        }
        lists.add("热点");
        lists.add("推荐");
        lists.add("动态");
        lists.add("搜索");
        lists.add("设置");
        lists.add("显示");
        lists.add("亮度");
        lists.add("WIFI");
        lists.add("来电显示");
        //自定义适配器
        // 侧滑新添
        ArrayAdapter madapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,lists);
        listviewdetail.setAdapter(madapter);
    }


    public void onLoad(){
        mXlv.stopLoadMore();
        mXlv.stopRefresh();
        mXlv.setRefreshTime("刚刚");
    }


    @Override
    public void onRefresh() {
        Toast.makeText(this,"下拉刷新",Toast.LENGTH_SHORT).show();
        pageNum=1;
        new AsyncTask(){


            @Override
            protected String doInBackground(String... strings) {
                NetWorkUtils netWorkUtils = new NetWorkUtils();
                String jsonStr = netWorkUtils.getJsonByHttpurlConnection(JSON_URL+pageNum);
                return jsonStr;
            }


            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                SuperClass fromJson = gson.fromJson(s, SuperClass.class);
                List list = fromJson.getData();


                //创建适配器
               adapter=new MyAdapter(list,MainActivity.this);
                mXlv.setAdapter(adapter);
                onLoad();
            }
        }.execute();


    }


    @Override
    public void onLoadMore() {
        Toast.makeText(this,"上拉加载",Toast.LENGTH_SHORT).show();
        pageNum++;
        new AsyncTask(){


            @Override
            protected String doInBackground(String... strings) {
                NetWorkUtils netWorkUtils = new NetWorkUtils();
                String jsonStr = netWorkUtils.getJsonByHttpurlConnection(JSON_URL+pageNum);
                return jsonStr;
            }


            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                SuperClass fromJson = gson.fromJson(s, SuperClass.class);
                List lists = fromJson.getData();


                list.addAll(lists);
                adapter.notifyDataSetChanged();
                onLoad();
            }
        }.execute();


    }

}

//需要继承的类

//XListView.java,XListViewFooter.java,XListViewHeader

//myapp加载图片

package lianxi.bawei.com.day10_xlistview_lianxi;


import android.app.Application;


import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;


/**
 * Created by 杜晓彤 on 2017/11/10.
 */


public class MyAppp extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(build);
    }
}

//setting里的内容


    day10_XListview_lianxi
   
    下拉刷新
    松开刷新数据
    正在加载...
    上次更新时间:
    查看更多
    松开载入更多

//请求网络

package lianxi.bawei.com.day10_xlistview_lianxi.utils;


import android.util.Log;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by 杜晓彤 on 2017/11/10.
 */


public class NetWorkUtils {
    public String getJsonByHttpurlConnection(String jsonUrl){
        URL url=null;
        HttpURLConnection urlConnection=null;
        String data="";
        try {
            url=new URL(jsonUrl);
            urlConnection= (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            int responseCode = urlConnection.getResponseCode();
            if(responseCode==200){
                InputStream inputStream = urlConnection.getInputStream();
                byte[] b=new byte[1024];
                int len=0;
                while((len=inputStream.read(b))!=-1){
                    String str=new String(b,0,len);
                    data+=str;
                }
                Log.d("TAG",data);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return data;
    }
}

//bean类

package lianxi.bawei.com.day10_xlistview_lianxi.bean;


import java.util.List;


/**
 * Created by 杜晓彤 on 2017/11/10.
 */


public class SuperClass {


    /**
     * result : success
     * data : [{"ID":"231f096c7908413fb614d959476707d1","TITLE":"九三学社普陀区委召开\u201c同心家园\u201d建设专项民主监督研讨会","SUBTITLE":"10月20日晚,九三学社普陀区委召开\u201c同心家园\u201d建设专项民主监督研讨会,会议由社","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_29_15_883_31506_a6ac2886bb494bc59dab22ac317c4289_1024-28jsxsptqwzktxjyjszxmzjdyth.jpg","FROMNAME":"九三学社上海市委","SHOWTIME":"2017-10-26","RN":11},{"ID":"2a75dc3a9a364417a7d7f59a2036885c","TITLE":"九三学社天津市委开展\u201c同心\u201d工程帮扶项目调研维保活动","SUBTITLE":"10月24日,九三学社天津市委秘书长张玉芳率队赴\u201c同心\u201d工程实践基地\u2014\u2014蓟州区孙","IMAGEURL":null,"FROMNAME":"九三学社天津市委","SHOWTIME":"2017-10-26","RN":12},{"ID":"3a497f2c81c34208888d96416f874bea","TITLE":"九三学社肇庆市委来宁调研科技产业创新","SUBTITLE":"9月23日,九三学社肇庆市委一行7人在肇庆市副市长、九三学社肇庆市委主委陈宣群的","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_55_17_840_58970_87ac1517b3ba450fb38fc037c31f5cf8_17102402.jpg","FROMNAME":"九三学社南京市委","SHOWTIME":"2017-10-26","RN":13},{"ID":"6a29252974d143e1aa97fe0899af86ad","TITLE":"九三学社河南省委莅宛开展\u201c农业供给侧机构性改革\u201d专题调研","SUBTITLE":"10月23日至24日,九三学社河南许昌支社受九三学社河南省委委托,在许昌支社副主","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_42_51_168_43789_d536872e37734afdb5515d5e27a5d915_zth.jpg","FROMNAME":"九三学社南阳市委","SHOWTIME":"2017-10-26","RN":14},{"ID":"437ae3490879497b87413665cec20018","TITLE":"赵金云参加省委统战部专题督查会","SUBTITLE":"10月25日上午,中共甘肃省委统战部副部长万泽刚带领督查组,督查各民主党派深入贯","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_53_39_212_04190_f774242e546c4d6ba37e2db024f10cee_DSC_2920.jpg","FROMNAME":"九三学社甘肃省委","SHOWTIME":"2017-10-26","RN":15},{"ID":"fea8a2d1f0bb4dd79b96644de11dbbdc","TITLE":"孟庆海到沈阳地质调查中心调研指导","SUBTITLE":"10月18日,在中国共产党第十九次全国代表大会开幕会的当天下午,九三学社辽宁省委","IMAGEURL":null,"FROMNAME":"九三学社辽宁省委","SHOWTIME":"2017-10-26","RN":16},{"ID":"3167ac92a95643418543c987364fdb59","TITLE":"关爱老人 杏林送暖","SUBTITLE":"九三学社天津市滨海新区区委开展医疗专家义诊活动","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/11_08_32_556_51643_1801d50243dc4e909f14dc7c985b09e9_10.19galrxlsn-2.jpg","FROMNAME":"九三学社天津市委","SHOWTIME":"2017-10-26","RN":17},{"ID":"2ba3a181ff7d4738b9632ba33c45e2c0","TITLE":"九三学社\u201c津豫合作\u201d项目第二期眼科影像诊断进展论坛举行","SUBTITLE":"10月13日至15日,九三学社\u201c津豫合作\u201d项目第二期眼科影像诊断进展论坛在河南省","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_56_22_659_87080_f642becc279845bbbe1f648dcdc7485b_10.15sfycxjsxsjyhzxmdeqykyxzdjzlt.jpg","FROMNAME":"九三学社天津市委","SHOWTIME":"2017-10-26","RN":18},{"ID":"cbc3e5f2f81c4425be2151df4442222f","TITLE":"愿使碧水绕古城 ","SUBTITLE":"\u2014\u2014九三学社开封市委调研全市\u201c一渠六河\u201d连通综合治理工程","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/11_15_38_065_15486_ea677af5dca0468fabe13f0c9828b0b0_dyxc2.jpg","FROMNAME":"九三学社开封市委","SHOWTIME":"2017-10-26","RN":19},{"ID":"3d519fb616da46828b86da3e29d654e2","TITLE":"九三学社洛阳市委赴陈吴调研脱贫攻坚民主监督","SUBTITLE":"10月24日,洛阳市政协副主席、九三学社洛阳市委主委肖宏滨,九三学社洛阳市委副主","IMAGEURL":null,"FROMNAME":"九三学社洛阳市委","SHOWTIME":"2017-10-25","RN":20},{"ID":"e2bd0fa0727142ab878502ce74403d39","TITLE":"九三学社保定市委 \u201c助力创城 爱心义诊\u201d活动走进阜平凹里","SUBTITLE":"10月22日,九三学社保定市委组织的\u201c助力创城,爱心义诊\u201d活动走进红色老区\u2014阜平","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/30/10_30_03_482_27494_7311cf7e217a445f9aabb18aff1c662d_yzxc2.jpg","FROMNAME":"九三学社保定市委","SHOWTIME":"2017-10-25","RN":21},{"ID":"0b1dba22928e45f7b130a38a267f4b1b","TITLE":"围绕中心 服务大局 用实际行动贯彻党的十九大精神","SUBTITLE":"九三学社四川省直工委参政议政委员会召开 \u201c完善住房租赁制度\u201d实务研讨会","IMAGEURL":null,"FROMNAME":"九三学社四川省委","SHOWTIME":"2017-10-25","RN":22},{"ID":"07698abb50c34859903afba0b93ac669","TITLE":"爱心公益 科普助力","SUBTITLE":"筑景希望小学科普站成立","IMAGEURL":null,"FROMNAME":"九三学社大连市委","SHOWTIME":"2017-10-25","RN":23},{"ID":"da40122b8d9e43caaf86bf1e62cc4545","TITLE":"九三学社安徽省委赴上海调研\u201c讲科普\u201d工作","SUBTITLE":"10月18日-20日,九三学社安徽省委专职副主委张腊梅带领社省委社会服务部、社省","IMAGEURL":null,"FROMNAME":"九三学社安徽省委","SHOWTIME":"2017-10-25","RN":24},{"ID":"d3d3491f40a4419facf6eb16ed6b32b4","TITLE":"\u201c百名专家科技下乡\u201d活动在玉溪举行","SUBTITLE":"本着科普服务大众,技术服务基层的理念,充分发挥九三学社和省科协的人才智力优势,1","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/30/10_26_55_023_41692_ae0f7ae110734deaa3c7bb952e389b63_qdysxc.jpg","FROMNAME":"九三学社玉溪市委","SHOWTIME":"2017-10-24","RN":25},{"ID":"0e3e03ce015e4b91b1f48b33f0f13a47","TITLE":"九三学社贵州省委赴青海开展社会服务工作调研","SUBTITLE":"9月25日至27日,社贵州省委秘书长李梓率九三学社贵州省地市组织负责人一行10人","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/15_20_34_099_69071_af51f272b1c84f10af08e84074920920_IMG_9296.jpg","FROMNAME":"九三学社青海省委","SHOWTIME":"2017-10-24","RN":26},{"ID":"2c8aedd364324b0fa7be47a83bb50cee","TITLE":"九三学社唐山华北理工大学医学支社开展骨质疏松日义诊活动","SUBTITLE":"10月20日上午,时值国际骨质疏松日,九三学社华北理工大学医学支社联合华北理工大","IMAGEURL":null,"FROMNAME":"九三学社唐山市委","SHOWTIME":"2017-10-24","RN":27},{"ID":"0ced86c5b29947e1b247593d205211dd","TITLE":"九三学社温州市委赴龙湾区开展\u201c最多跑一次\u201d改革调研督查回头看活动","SUBTITLE":"10月13日,温州市政协副主席、九三学社温州市委主委黄少铭率督查组赴龙湾区开展\u201c","IMAGEURL":null,"FROMNAME":"九三学社浙江省委","SHOWTIME":"2017-10-24","RN":28},{"ID":"5bc9f63e1d8d4ec8beffcc0529d5000c","TITLE":"湖州市政协就九三学社湖州市委重点提案开展专题调研","SUBTITLE":"10月12日,湖州市政协就九三学社湖州市委重点提案《关于加大动物检疫领域安全保障","IMAGEURL":null,"FROMNAME":"九三学社浙江省委","SHOWTIME":"2017-10-24","RN":29},{"ID":"78b0ca0dfe8542ae9c809f4230ae893e","TITLE":"九三学社上海交通大学青年委员会赴金山开展调研考察","SUBTITLE":"10月14日,九三学社上海交通大学青年委员会组织青年社员赴金山开展调研考察活动,","IMAGEURL":"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/15_35_04_642_47861_0bb48a4ee30d45ff81d75532c994ed92_1019-17jsxsshjtdxqnwyhfjskzdykc01.jpg","FROMNAME":"九三学社上海市委","SHOWTIME":"2017-10-24","RN":30}]
     */


    private String result;
    private List data;


    public String getResult() {
        return result;
    }


    public void setResult(String result) {
        this.result = result;
    }


    public List getData() {
        return data;
    }


    public void setData(List data) {
        this.data = data;
    }


    public static class DataBean {
        /**
         * ID : 231f096c7908413fb614d959476707d1
         * TITLE : 九三学社普陀区委召开“同心家园”建设专项民主监督研讨会
         * SUBTITLE : 10月20日晚,九三学社普陀区委召开“同心家园”建设专项民主监督研讨会,会议由社
         * IMAGEURL : http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/27/10_29_15_883_31506_a6ac2886bb494bc59dab22ac317c4289_1024-28jsxsptqwzktxjyjszxmzjdyth.jpg
         * FROMNAME : 九三学社上海市委
         * SHOWTIME : 2017-10-26
         * RN : 11
         */


        private String ID;
        private String TITLE;
        private String SUBTITLE;
        private String IMAGEURL;
        private String FROMNAME;
        private String SHOWTIME;
        private int RN;


        public String getID() {
            return ID;
        }


        public void setID(String ID) {
            this.ID = ID;
        }


        public String getTITLE() {
            return TITLE;
        }


        public void setTITLE(String TITLE) {
            this.TITLE = TITLE;
        }


        public String getSUBTITLE() {
            return SUBTITLE;
        }


        public void setSUBTITLE(String SUBTITLE) {
            this.SUBTITLE = SUBTITLE;
        }


        public String getIMAGEURL() {
            return IMAGEURL;
        }


        public void setIMAGEURL(String IMAGEURL) {
            this.IMAGEURL = IMAGEURL;
        }


        public String getFROMNAME() {
            return FROMNAME;
        }


        public void setFROMNAME(String FROMNAME) {
            this.FROMNAME = FROMNAME;
        }


        public String getSHOWTIME() {
            return SHOWTIME;
        }


        public void setSHOWTIME(String SHOWTIME) {
            this.SHOWTIME = SHOWTIME;
        }


        public int getRN() {
            return RN;
        }


        public void setRN(int RN) {
            this.RN = RN;
        }
    }
}


//适配器

package lianxi.bawei.com.day10_xlistview_lianxi.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.nostra13.universalimageloader.core.ImageLoader;


import java.util.Date;
import java.util.List;


import lianxi.bawei.com.day10_xlistview_lianxi.R;
import lianxi.bawei.com.day10_xlistview_lianxi.bean.SuperClass;


/**
 * Created by 杜晓彤 on 2017/11/10.
 */


public class MyAdapter extends BaseAdapter {
    private List list;
    private Context context;


    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }


    @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 holder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.list_view_item,null);
            holder=new ViewHolder();
            holder.imageView=(ImageView) convertView.findViewById(R.id.imageView);
            holder.textView=(TextView)convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }
        holder.textView.setText(list.get(position).getTITLE());
        String url=list.get(position).getIMAGEURL();
        ImageLoader.getInstance().displayImage(url,holder.imageView);


        return convertView;
    }


    public class ViewHolder{
        private TextView textView;
        private ImageView imageView;
    }
}

//布局activity1


    android:id="@+id/dl"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="lianxi.bawei.com.day10_xlistview_lianxi.MainActivity">


           android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/xlv"/>


           android:id="@+id/listviewdetail"
       android:layout_width="280dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:background="#fff"
       android:divider="#000"
       android:dividerHeight="1dp"
       android:padding="10dp"
       >



//导入的布局

xlistview_footer.xml,xlistview_header.xml


//activity2


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
            android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        />
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="22sp"
        android:text="TextView"
        />

你可能感兴趣的:(Xlistview上拉刷新和下拉加载)