Json解析网络获取Viewpager图片glide下载图片

MainActivity


package com.example.administrator.myviewpagerdemos;



import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {
    private ViewPager vip;
    private int cur = 0;
    private ImageView mImg,mImg1,mImg2,mImg3;
    private List<ImageView> imgList = new ArrayList<>();
    private boolean isAuto = true;
    private boolean isTouch = false;
    public static final MediaType MEDIA_TYPE_MARKDOWN
            = MediaType.parse("text/x-markdown; charset=utf-8");
    private OkHttpClient mOkHttpClient;
    private String path = "http://litchiapi.jstv.com/api/getTops?limit=5&column=0&val=F467412B44B421716757A6B2D7635B4A";
    private ImageView img;
//    private View[] views=new View[arr.length];
    //用来进行求魔取余
    private int index=4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vip = (ViewPager) findViewById(R.id.vip_one);
        mImg=(ImageView) findViewById(R.id.mImg);
        mImg1=(ImageView) findViewById(R.id.mImg1);
        mImg2=(ImageView) findViewById(R.id.mImg2);
        mImg3=(ImageView) findViewById(R.id.mImg3);


        initView();
        initEvent();
    }
    private void cleanBotton(){
        mImg.setImageResource(R.mipmap.vote_n_can_n);
        mImg1.setImageResource(R.mipmap.vote_n_can_n);
        mImg2.setImageResource(R.mipmap.vote_n_can_n);
        mImg3.setImageResource(R.mipmap.vote_n_can_n);
    }


    private void initEvent() {


        vip.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub


                cur = arg0;
                cleanBotton();
                switch (arg0%index) {
                    case 0:
                        mImg.setImageResource(R.mipmap.vote_n_can_y);
                        break;
                    case 1:
                        mImg1.setImageResource(R.mipmap.vote_n_can_y);
                        break;
                    case 2:
                        mImg2.setImageResource(R.mipmap.vote_n_can_y);
                        break;
                    case 3:
                        mImg3.setImageResource(R.mipmap.vote_n_can_y);
                        break;


                }
            }


            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub


            }


            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                /*
                 * 判断是否存在手动滑动 挡手动滑动时将inTouch的编制设为true
*/
                if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
                    isTouch = false;
                } else {
                    isTouch = true;
                }
            }
        });
    }


    private void initView() {
        //创建okHttpClient对象
        mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url(path)
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = s;
                handler.sendMessage(message);
            }
        });
    }


    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (isAuto) {
                try {
                    thread.sleep(1500);
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                //如果手动滑动时不处理
                if (isTouch) {
                    return;
                }
                //如果是自动轮播时,让代表显示页面的指示位置+1;
                cur++;
                vip.setCurrentItem(cur);
            }
            if (msg.what == 1) {
                String s1 = (String) msg.obj;
                try {
                    JSONObject jo = new JSONObject(s1);
                    JSONObject paramz = jo.optJSONObject("paramz");
                    JSONArray ja = paramz.optJSONArray("tops");
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jb = ja.optJSONObject(i);
                        String photo = jb.getString("photo");
                        String subject = jb.getString("subject");
                        User u = new User();
                        u.setPhoto(photo);
                        u.setSubject(subject);
                        View v = View.inflate(MainActivity.this, R.layout.viewitem, null);
                        img = (ImageView) v.findViewById(R.id.img1);
                        img.setScaleType(ImageView.ScaleType.FIT_XY);
                        Glide.with(MainActivity.this).load("http://litchiapi.jstv.com" + u.getPhoto()).into(img);
                        imgList.add(img);
                    }
                    MyAdapter adapter = new MyAdapter(imgList);
                    vip.setAdapter(adapter);
                    thread.start();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };

}

   




适配器


package com.example.administrator.myviewpagerdemos;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;


/*
 2017.9.8
 */
public class MyAdapter extends PagerAdapter {
    private List<ImageView> imgList=new ArrayList<ImageView>();
    public MyAdapter(List<ImageView> imgList){
        this.imgList=imgList;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Integer.MAX_VALUE;
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0==arg1;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        int p=position%imgList.size();
        ImageView img=imgList.get(p);
        ViewParent parent=img.getParent();
        if(parent!=null){
            ViewGroup vg=(ViewGroup) parent;
            vg.removeView(img);
        }
        container.addView(imgList.get(p));
        return imgList.get(p);


    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        int p=position%imgList.size();
        container.removeView(imgList.get(p));
    }
}




User类

package com.example.administrator.myviewpagerdemos;


/**
 * Created by Administrator on 2017/8/29.
 */


public class User {
    String photo;


    public String getSubject() {
        return subject;
    }


    public void setSubject(String subject) {
        this.subject = subject;
    }


    public String getPhoto() {
        return photo;
    }


    public void setPhoto(String photo) {
        this.photo = photo;
    }


    String subject;


}


XML

activiaty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.myviewpagerdemos.MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <android.support.v4.view.ViewPager
                android:id="@+id/vip_one"
                android:layout_width="match_parent"
                android:layout_height="240dp" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="30dp"
                android:orientation="horizontal">


                <ImageView
                    android:id="@+id/mImg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/vote_n_can_y" />


                <ImageView
                    android:id="@+id/mImg1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/vote_n_can_n" />


                <ImageView
                    android:id="@+id/mImg2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/vote_n_can_n" />


                <ImageView
                    android:id="@+id/mImg3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/vote_n_can_n" />
            </LinearLayout>
        </LinearLayout>


    </ScrollView>
</RelativeLayout>



view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img1"
    />


</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值