xlistview

来源:互联网 发布:知客是什么工作 编辑:程序博客网 时间:2024/05/29 14:14
import android.os.AsyncTask;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.example.bean.ListBase;import com.example.xlist.XListView;import com.google.gson.Gson;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class MainActivity extends AppCompatActivity {    private XListView xListView;    ListBase li;    List<Mydete.DataBean> data=new ArrayList<>();    int pasize=1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        xListView = (XListView) findViewById(R.id.xlv);        xListView.setPullLoadEnable(true);        xListView.setPullRefreshEnable(true);        sta();        xListView.setXListViewListener(new XListView.IXListViewListener() {            @Override            public void onRefresh() {                flash();                Date da=new Date(System.currentTimeMillis()) ;                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");                xListView.setRefreshTime(simpleDateFormat.format(da));            }            @Override            public void onLoadMore() {                sta();            }        });    }    public void sta(){        AsyncTask<Void,Void,String> as=new AsyncTask<Void, Void, String>() {            @Override            protected void onPostExecute(String s) {                Gson gson=new Gson();                Mydete mydete = gson.fromJson(s, Mydete.class);                data.addAll(mydete.getData());           if(li==null){                li=new ListBase(data,MainActivity.this);                xListView.setAdapter(li);            }else{                li.notifyDataSetChanged();            }            xListView.stopLoadMore();            }            @Override            protected String doInBackground(Void... voids) {                String path="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";                try {                    URL url=new URL(path);                   HttpURLConnection connection= (HttpURLConnection) url.openConnection();                    connection.setRequestMethod("GET");                    connection.setReadTimeout(5000);                    connection.setConnectTimeout(5000);                    int code = connection.getResponseCode();                    if(code==200){                        InputStream stream = connection.getInputStream();                        BufferedReader bu=new BufferedReader(new InputStreamReader(stream,"utf-8"));                        String na=null;                        StringBuffer st=new StringBuffer();                        while ((na=bu.readLine())!=null){                            st.append(na);                        }                        bu.close();                        return st.toString();                    }                } catch (Exception e) {                    e.printStackTrace();                }                return null;            }        };        as.execute();    }    public void flash(){        pasize++;        AsyncTask<Void,Void,String> as=new AsyncTask<Void, Void, String>() {            @Override            protected void onPostExecute(String s) {                Gson gson=new Gson();                Mydete mydete = gson.fromJson(s, Mydete.class);                data.addAll(0,mydete.getData());                if(li==null){                    li=new ListBase(data,MainActivity.this);                    xListView.setAdapter(li);                }else{                    li.notifyDataSetChanged();                }                xListView.stopRefresh();            }            @Override            protected String doInBackground(Void... voids) {                String path="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+pasize;                try {                    URL url=new URL(path);                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();                    connection.setRequestMethod("GET");                    connection.setReadTimeout(5000);                    connection.setConnectTimeout(5000);                    int code = connection.getResponseCode();                    if(code==200){                        InputStream stream = connection.getInputStream();                        BufferedReader bu=new BufferedReader(new InputStreamReader(stream,"utf-8"));                        String na=null;                        StringBuffer st=new StringBuffer();                        while ((na=bu.readLine())!=null){                            st.append(na);                        }                        bu.close();                        return st.toString();                    }                } catch (Exception e) {                    e.printStackTrace();                }                return null;            }        };        as.execute();    }

}

适配器

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.example.wanghao01.Mydete;import com.example.wanghao01.R;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by DELL on 2017/10/11. */public class ListBase extends BaseAdapter {    List<Mydete.DataBean> data;    Context cex;    public ListBase(List<Mydete.DataBean> data,Context cex) {        this.data = data;        this.cex=cex;    }    @Override    public int getViewTypeCount() {        return 2;    }    @Override    public int getItemViewType(int position) {        if(position%2==0){            return 0;        }        return 1;    }    @Override    public int getCount() {        return data.size();    }    @Override    public Object getItem(int i) {        return data.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        if(getItemViewType(i)==0){        ViewHouder hd;            if(view==null){                view=View.inflate(cex, R.layout.xlistbase,null);                hd=new ViewHouder();                hd.name=view.findViewById(R.id.name);              hd.img=view.findViewById(R.id.img);                view.setTag(hd);            }else{               hd= (ViewHouder)view.getTag();            }            Mydete.DataBean dataBean = data.get(i);            hd.name.setText(dataBean.getTitle());            ImageLoader.getInstance().displayImage(dataBean.getImg(),hd.img,ImgHouder.dis());        }else if(getItemViewType(i)==1){            ViewHouder hd;            if(view==null){                view=View.inflate(cex, R.layout.xlistbase1,null);                hd=new ViewHouder();                hd.name=view.findViewById(R.id.name);                hd.img=view.findViewById(R.id.img);                view.setTag(hd);            }else{                hd= (ViewHouder)view.getTag();            }            Mydete.DataBean dataBean = data.get(i);            hd.name.setText(dataBean.getTitle());            ImageLoader.getInstance().displayImage(dataBean.getImg(),hd.img,ImgHouder.dis());        }        return view;    }    class ViewHouder{        TextView name;        ImageView img;    }}





原创粉丝点击