Xutils请求数据imageloader加载图片+网络判断

时间:2021-03-03 22:15:55
添加依赖:
compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
compile 'com.google.code.gson:gson:2.8.1'
compile files('libs/universal-image-loader-1.9.5.jar')
compile 'org.xutils:xutils:3.5.0'

在AndroidManifest中添加网络权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

---------------------------------------------------------
package fragment;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.GridView;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import com.example.e.yuekaolianxi.PingDaoGuanLi;import com.example.e.yuekaolianxi.R;import com.example.e.yuekaolianxi.Utils;import com.google.gson.Gson;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import java.util.ArrayList;import java.util.List;import bean.Bean;public class Fragment2_ShiPing extends Fragment implements View.OnClickListener {    private ViewPager viewPager;    private ImageView[] points;    private int count = 0;    private String s;    private List<Bean.DataBean.ComicsBean> list;    private MyAdapter adapter;    private GridView gridView;    private Button pingdao;    private String[] images = {            "http://f2.kkmh.com/image/161129/5lw6qrrjf.webp-w750.jpg",            "http://f2.kkmh.com/image/160127/ord904ylf.webp-w750.jpg",            "http://f2.kkmh.com/image/170623/6c5i8d00h.webp-w750.jpg",            "http://f2.kkmh.com/image/170216/4qksgntjb.webp-w750.jpg",            "http://f2.kkmh.com/image/170616/miaujen7x.webp-w750.jpg"};    Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            viewPager.setCurrentItem(count);            count++;            count = count % images.length;            send();        }    };    Handler mHandler1 = new Handler() {        @Override        public void handleMessage(Message msg) {            s = msg.obj.toString();            Gson gson = new Gson();            Bean bean = gson.fromJson(s, Bean.class);            list.addAll(bean.getData().getComics());            adapter.notifyDataSetChanged();        }    };    private String path = "http://api.kuaikanmanhua.com/v1/daily/comic_lists/1489334400?since=0&gender=0&sa_event=eyJwcm9qZWN0I";    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment2_faxian, container, false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        gridView = (GridView) getView().findViewById(R.id.gridView);        pingdao = (Button) getView().findViewById(R.id.pingdao);        initView();        points = new ImageView[5];        points[0] = (ImageView) getView().findViewById(R.id.image01);        points[1] = (ImageView) getView().findViewById(R.id.image02);        points[2] = (ImageView) getView().findViewById(R.id.image03);        points[3] = (ImageView) getView().findViewById(R.id.image04);        points[4] = (ImageView) getView().findViewById(R.id.image05);        for (ImageView image : points) {            image.setOnClickListener(this);        }        updatePoint(count);        send();        if (isNetworkAvailable(getActivity())) {            loder();        }        list = new ArrayList<>();        adapter = new MyAdapter();        gridView.setAdapter(adapter);       //跳转到频道管理页面        pingdao.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(getActivity(), PingDaoGuanLi.class);                startActivity(intent);            }        });    }    private void loder() {        new Thread() {            @Override            public void run() {                String s = Utils.parmrs(path);                Message msg = Message.obtain();                msg.obj = s;                mHandler1.sendMessage(msg);            }        }.start();    }    private void updatePoint(int position) {        for (ImageView image : points) {            image.setImageResource(R.drawable.nor);        }        points[position].setImageResource(R.drawable.sel);    }    private void initView() {        viewPager = (ViewPager) getView().findViewById(R.id.viewPager);        final ViewPagerAdapter adapter = new ViewPagerAdapter();        viewPager.setAdapter(adapter);        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            private boolean flag;            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {                updatePoint(position);            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }    public void send() {        new Thread() {            @Override            public void run() {                mHandler.sendEmptyMessageDelayed(count, 2000);            }        }.start();    }    @Override    public void onClick(View v) {        mHandler.removeCallbacksAndMessages(null);        int intex = Integer.valueOf(v.getTag().toString());        viewPager.setCurrentItem(intex, false);        count = intex + 1;        send();    }    class ViewPagerAdapter extends PagerAdapter {        @Override        public int getCount() {            return images.length;        }        @Override        public boolean isViewFromObject(View view, Object object) {            return view == object;        }        @Override        public Object instantiateItem(ViewGroup container, int position) {            ImageView imageView = new ImageView(getActivity());            Glide.with(getActivity())                    .load(images[position])//                    .placeholder(R.drawable.nor)                    .into(imageView);            container.addView(imageView);            return imageView;        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView((View) object);        }    }    public static boolean isNetworkAvailable(Context context) {        ConnectivityManager connectivity = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        if (connectivity != null) {            NetworkInfo info = connectivity.getActiveNetworkInfo();            if (info != null && info.isConnected()) {                // 当前网络是连接的                if (info.getState() == NetworkInfo.State.CONNECTED) {                    // 当前所连接的网络可用                    return true;                }            }        }        return false;    }    class MyAdapter extends BaseAdapter {        ImageLoader imageLoader;        DisplayImageOptions options;        public MyAdapter() {            imageLoader = ImageLoader.getInstance();            ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(getActivity());            imageLoader.init(config);            options = new DisplayImageOptions.Builder()                    .showImageOnLoading(R.mipmap.ic_launcher)                    .build();        }        @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) {            Bean.DataBean.ComicsBean bean = list.get(position);            Holder holder;            if (convertView == null) {                holder = new Holder();                convertView = View.inflate(getActivity(), R.layout.fragment2_item, null);                holder.image = (ImageView) convertView.findViewById(R.id.imageView);                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);                holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);                convertView.setTag(holder);            } else {                holder = (Holder) convertView.getTag();            }            holder.tv1.setText(bean.getTopic().getTitle());            holder.tv2.setText(bean.getTitle());            imageLoader.displayImage(bean.getTopic().getCover_image_url(), holder.image, options);            return convertView;        }    }    class Holder {        ImageView image;        TextView tv1, tv2;    }    @Override    public void onDestroy() {        super.onDestroy();        mHandler.removeCallbacksAndMessages(null);    }}