xlistview

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值