轮播图+xlistview异步请求的数据+imglode获取图片数据

package com.example.zkt;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.zkt.view.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.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
    int index;
    private XListView xl;
    private MyBase myBase;
    private LinearLayout lt;
    private Mydapter mydapter;
    private ViewPager vp;
    private ListView lv;
    private List<JsonBean.DataBean> list=new ArrayList<>();
    //数据源
    int ids[]=new int[]{R.drawable.r,
    R.drawable.tt,R.drawable.w,R.drawable.x};

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

            index++;
        while (index>=5)
        {
            index=0;
        }
            vp.setCurrentItem(index);
        handler.sendEmptyMessageDelayed(0,2000);
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        lt = (LinearLayout) findViewById(R.id.lt);

        intview();
        intData();
        hands();

        myBase = new MyBase();
        vp.setAdapter(myBase);
        initcot();

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<lt.getChildCount();i++)
                {
                    ImageView img = (ImageView) lt.getChildAt(i);
                if (i==position%ids.length)
                {
                    img.setImageResource(R.drawable.point_selected);

                }else {
                    img.setImageResource(R.drawable.point_mormal);
                }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        handler.sendEmptyMessageDelayed(0,2000);
    }

    private void hands() {
        Handler handler = new Handler();
       mydapter = new Mydapter(MainActivity.this,list);
        xl.setAdapter(mydapter);
    }

    private void intview() {
        xl = (XListView) findViewById(R.id.xl);
        xl.setPullLoadEnable(true);
        xl.setXListViewListener(this);
    }

    private void intData() {
    new Mystakt().execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1");
    }
class Mystakt extends AsyncTask<String,Void,String>{



    @Override
    protected String doInBackground(String... strings) {
        URL url = null;
        try {
            url = new URL(strings[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int code = connection.getResponseCode();
            if (code==200)
            {
                InputStream inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();
                String str ="";
                while ((str=bufferedReader.readLine())!=null) {
                    stringBuffer.append(str);
                }
                return stringBuffer.toString();

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

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        JsonBean bean = gson.fromJson(s, JsonBean.class);
        List<JsonBean.DataBean> data = bean.getData();
        list.addAll(data);
      mydapter.notifyDataSetChanged();
    }
}
    private void initcot() {

        for (int i = 0; i < ids.length; i++) {
            ImageView img = new ImageView(MainActivity.this);
            if (i == 0) {
                img.setImageResource(R.drawable.point_selected);

            } else {
                img.setImageResource(R.drawable.point_mormal);

            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
           params.setMargins(10,5,10,5);
            lt.addView(img, params);
        }
    }

    public  void  onLoad()
    {
        xl.stopRefresh();
        xl.stopLoadMore();

        xl.setRefreshTime("刚刚");
    }
    @Override
    public void onRefresh() {
        list.clear();
        intData();
        onLoad();

    }

    @Override
    public void onLoadMore() {
        intData();
        onLoad();
    }

    class MyBase extends PagerAdapter{

            @Override
            public int getCount() {
                return ids.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                ImageView img = new ImageView(MainActivity.this);
                img.setImageResource(ids[position]);
                container.addView(img);
                return img;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        }

}
2.Bean类
package com.example.zkt;

import java.util.List;

/**
 * author:Created by WangZhiQiang on 2017/9/14.
 */

class JsonBean {


   /**
     * code : 1
     * height : 0
     * width : 0
     * success : true
     * data : [{"userAge":25,"topTime":1458873660000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i2/199?a=1505392195906","createTime":1458873458000,"hxKey":"c660d6a8c922852146cc3725a64208ff","img":"http://img1.yulin520.com/news/EN9I5SQNRFS0OMQ8N7U5.jpg#498_589","yulin":"75187","star":41,"title":"翩翩:愿我的生活如名字般起舞","occupation":"","userImg":"http://img1.yulin520.com/news/EN9I5SQNRFS0OMQ8N7U5.jpg#498_589","impressEditId":199,"impressType":10,"replyTimes":7,"remark":"","click":0,"introduction":"91年白羊座/出生地:江苏扬州/金融行业/理想型:孝顺,有担当,有才华,有趣\n","source":0,"userName":"进击的女战士","reporter":18,"status":1,"content":""},{"userAge":27,"topTime":1461057379000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/200?a=1505392195906","createTime":1458889672000,"hxKey":"73c68797a138ae0442349d3184e9280d","img":"http://img1.yulin520.com/news/1N94DU7KMJI0O6Q4PEF3.jpg#1334_1000","yulin":"74392","star":31,"title":"Jason:是设计师,也是体育健将","occupation":"房地产","userImg":"http://img1.yulin520.com/news/1N94DU7KMJI0O6Q4PEF3.jpg#1334_1000","impressEditId":200,"impressType":0,"replyTimes":8,"remark":"","click":0,"introduction":"89年巨蟹座/超热爱运动/四川好男人/设计师\n","source":0,"userName":"Jason","reporter":0,"status":1,"content":""},{"userAge":25,"topTime":1460997554000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i1/196?a=1505392195906","createTime":1458713922000,"hxKey":"8caa005c7cb4967e918c3dfcbee367aa","img":"http://img1.yulin520.com/news/ON7G30U57FS0OSWRZ88Z.jpg#982_857","yulin":"74356","star":30,"title":"徐凌云:多才多艺的金融才俊","occupation":"证劵投资","userImg":"http://img1.yulin520.com/news/ON7G30U57FS0OSWRZ88Z.jpg#982_857","impressEditId":196,"impressType":0,"replyTimes":14,"remark":"","click":0,"introduction":"91年金牛座/阳光开朗身材好/会拉手风琴/略微御姐控/金融系研究生","source":0,"userName":"阿豪","reporter":0,"status":1,"content":""},{"userAge":23,"topTime":1460995023000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i1/197?a=1505392195906","createTime":1458716304000,"hxKey":"5888b36659cbcf17c47b675186ef4a6b","img":"http://img1.yulin520.com/news/5N7044YIEFS0OAN0KQDW.jpg#798_634","yulin":"74920","star":54,"title":"周大大:温柔的射手美少女","occupation":"银行","userImg":"http://img1.yulin520.com/news/5N7044YIEFS0OAN0KQDW.jpg#798_634","impressEditId":197,"impressType":0,"replyTimes":12,"remark":"","click":0,"introduction":"93年射手座/颜值秒杀奖选手/温柔温柔温柔\n","source":0,"userName":"周大大","reporter":0,"status":1,"content":""},{"userAge":28,"topTime":1459138642000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i2/190?a=1505392195906","createTime":1458618361000,"hxKey":"952538d5ad9db22f3b33a05b6d73fe83","img":"http://img1.yulin520.com/news/0N5KVD8MA5S0OCYMN4Y3.jpg#519_600","yulin":"74731","star":75,"title":"徐掌柜:海归创业的白富美","occupation":"电子商务","userImg":"http://img1.yulin520.com/news/0N5KVD8MA5S0OCYMN4Y3.jpg#519_600","impressEditId":190,"impressType":10,"replyTimes":19,"remark":"","click":0,"introduction":"80后天秤座/深圳创二代/理想型:单纯善良,积极乐观,幽默感,有活力\n","source":0,"userName":"Melinda","reporter":0,"status":1,"content":""},{"userAge":25,"topTime":1460993895000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i1/191?a=1505392195906","createTime":1458618345000,"hxKey":"72cf0259e49aca7163b5a4098bdd0e32","img":"http://img1.yulin520.com/news/AN5EVOLIC5S0O6PQI5WW.jpg#640_962","yulin":"74407","star":40,"title":"余露瑶:独立随性的个性妞儿","occupation":"互联网","userImg":"http://img1.yulin520.com/news/AN5EVOLIC5S0O6PQI5WW.jpg#640_962","impressEditId":191,"impressType":0,"replyTimes":11,"remark":"","click":0,"introduction":"91年处女座/勇敢有个性/爱旅行/更爱挑战极限/互联网产品经理","source":0,"userName":"瑶瑶","reporter":0,"status":1,"content":""},{"userAge":25,"topTime":1460997981000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/192?a=1505392195906","createTime":1458633007000,"hxKey":"b464191f257573c383ba78f3028b6d44","img":"http://img1.yulin520.com/news/FN602UEVJ5S0O9UN4LUR.jpg#533_800","yulin":"74326","star":23,"title":"李俊:说走就走的腾讯设计师","occupation":"互联网","userImg":"http://img1.yulin520.com/news/FN602UEVJ5S0O9UN4LUR.jpg#533_800","impressEditId":192,"impressType":0,"replyTimes":8,"remark":"","click":0,"introduction":"91年白羊座/真诚幽默骚年/说走就走妥妥的/腾讯设计师","source":0,"userName":"小俊","reporter":0,"status":1,"content":""},{"userAge":28,"topTime":1460997908000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/193?a=1505392195906","createTime":1458636313000,"hxKey":"7d31135a4f9f047a58c409f3aa7c9f47","img":"http://img1.yulin520.com/news/2N613OXUB5S0OJNXSD65.jpg#655_600","yulin":"74215","star":27,"title":"王文龙:能开玩笑能背锅?没错是我!","occupation":"互联网","userImg":"http://img1.yulin520.com/news/2N613OXUB5S0OJNXSD65.jpg#655_600","impressEditId":193,"impressType":0,"replyTimes":8,"remark":"","click":0,"introduction":"88年巨蟹座/沉稳幽默/有梦想有计划/互联网运营","source":0,"userName":"William","reporter":0,"status":1,"content":""},{"userAge":27,"topTime":1460997820000,"recommend":1,"url":"http://www.yulin520.com/a2a/h5/impress/i1/194?a=1505392195906","createTime":1458638981000,"hxKey":"d0f3d8a428ba401606ed00097eef1b4b","img":"http://img1.yulin520.com/news/CN6U5S5C2FS0OM7392AT.jpg#589_598","yulin":"74359","star":23,"title":"张翔:跑半马、骑行,活力派工程师","occupation":"计算机软件","userImg":"http://img1.yulin520.com/news/CN6U5S5C2FS0OM7392AT.jpg#589_598","impressEditId":194,"impressType":0,"replyTimes":9,"remark":"","click":0,"introduction":"89年水瓶座/略闷骚大水瓶/各种运动都在行/疼女友好男人/IT工程师\n","source":0,"userName":"veen","reporter":0,"status":1,"content":""},{"userAge":26,"topTime":1460997699000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/195?a=1505392195906","createTime":1458640894000,"hxKey":"0f56a52878fdc7b35f337855a4d4a427","img":"http://img1.yulin520.com/news/4N6F611P3FS0OLOCZ0CK.jpg#500_600","yulin":"74209 ","star":24,"title":"黄建思:幽默大方的常德伢子","occupation":"","userImg":"http://img1.yulin520.com/news/4N6F611P3FS0OLOCZ0CK.jpg#500_600","impressEditId":195,"impressType":0,"replyTimes":10,"remark":"","click":0,"introduction":"90年 白羊座 /幽默大方 /成熟稳重 /有点大男子主义 /软件工程师 ","source":0,"userName":"偲","reporter":0,"status":1,"content":""}]
     * message : null
     */

    private int code;
    private int height;
    private int width;
    private boolean success;
    private Object message;
    private List<DataBean> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public List<DataBean> getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * userAge : 25
         * topTime : 1458873660000
         * recommend : 1
         * url : http://www.yulin520.com/a2a/h5/impress/i2/199?a=1505392195906
         * createTime : 1458873458000
         * hxKey : c660d6a8c922852146cc3725a64208ff
         * img : http://img1.yulin520.com/news/EN9I5SQNRFS0OMQ8N7U5.jpg#498_589
         * yulin : 75187
         * star : 41
         * title : 翩翩:愿我的生活如名字般起舞
         * occupation :
         * userImg : http://img1.yulin520.com/news/EN9I5SQNRFS0OMQ8N7U5.jpg#498_589
         * impressEditId : 199
         * impressType : 10
         * replyTimes : 7
         * remark :
         * click : 0
         * introduction : 91年白羊座/出生地:江苏扬州/金融行业/理想型:孝顺,有担当,有才华,有趣

         * source : 0
         * userName : 进击的女战士
         * reporter : 18
         * status : 1
         * content :
         */

        private int userAge;
        private long topTime;
        private int recommend;
        private String url;
        private long createTime;
        private String hxKey;
        private String img;
        private String yulin;
        private int star;
        private String title;
        private String occupation;
        private String userImg;
        private int impressEditId;
        private int impressType;
        private int replyTimes;
        private String remark;
        private int click;
        private String introduction;
        private int source;
        private String userName;
        private int reporter;
        private int status;
        private String content;

        public int getUserAge() {
            return userAge;
        }

        public void setUserAge(int userAge) {
            this.userAge = userAge;
        }

        public long getTopTime() {
            return topTime;
        }

        public void setTopTime(long topTime) {
            this.topTime = topTime;
        }

        public int getRecommend() {
            return recommend;
        }

        public void setRecommend(int recommend) {
            this.recommend = recommend;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public long getCreateTime() {
            return createTime;
        }

        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }

        public String getHxKey() {
            return hxKey;
        }

        public void setHxKey(String hxKey) {
            this.hxKey = hxKey;
        }

        public String getImg() {
            return img;
        }

        public void setImg(String img) {
            this.img = img;
        }

        public String getYulin() {
            return yulin;
        }

        public void setYulin(String yulin) {
            this.yulin = yulin;
        }

        public int getStar() {
            return star;
        }

        public void setStar(int star) {
            this.star = star;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getOccupation() {
            return occupation;
        }

        public void setOccupation(String occupation) {
            this.occupation = occupation;
        }

        public String getUserImg() {
            return userImg;
        }

        public void setUserImg(String userImg) {
            this.userImg = userImg;
        }

        public int getImpressEditId() {
            return impressEditId;
        }

        public void setImpressEditId(int impressEditId) {
            this.impressEditId = impressEditId;
        }

        public int getImpressType() {
            return impressType;
        }

        public void setImpressType(int impressType) {
            this.impressType = impressType;
        }

        public int getReplyTimes() {
            return replyTimes;
        }

        public void setReplyTimes(int replyTimes) {
            this.replyTimes = replyTimes;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        public int getClick() {
            return click;
        }

        public void setClick(int click) {
            this.click = click;
        }

        public String getIntroduction() {
            return introduction;
        }

        public void setIntroduction(String introduction) {
            this.introduction = introduction;
        }

        public int getSource() {
            return source;
        }

        public void setSource(int source) {
            this.source = source;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public int getReporter() {
            return reporter;
        }

        public void setReporter(int reporter) {
            this.reporter = reporter;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}

3.适配器
package com.example.zkt;

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.List;

/**
 * author:Created by WangZhiQiang on 2017/9/14.
 */

class Mydapter extends BaseAdapter{
    Context context;
    List<JsonBean.DataBean> list;

    public Mydapter(Context context, List<JsonBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Hodler hodel;
        if (view==null){
            hodel = new Hodler();
            view = view.inflate(context, R.layout.lv, null);
            hodel.textview=(TextView)view.findViewById(R.id.textView1);
            hodel.text=(TextView) view.findViewById(R.id.textView2);
            hodel.imageView1=(ImageView) view.findViewById(R.id.imageView);
            view.setTag(hodel);

        }
        else {
            hodel = (Hodler) view.getTag();
        }
        hodel.text.setText(list.get(i).getTitle());
        hodel.textview.setText(list.get(i).getOccupation());

        ImageLoader.getInstance().displayImage(list.get(i).getImg(),hodel.imageView1);



        return view;
    }
    class Hodler{

        public TextView textview;
        public TextView text;
        public ImageView imageView1;
    }
}
//ImgLode
package com.example.zkt;

import android.app.Application;

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

/**
 * author:Created by WangZhiQiang on 2017/9/14.
 */

public class Img extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(configuration);
    }
}
//清单文件也一定要写

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值