网络获取图片 无限轮播




//注意写imagelaoder缓存图片,要不然报错!!!!!!!
public class Tab_frag_03 extends Fragment {

    private String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    private FlyBanner fly;
    private ViewPager lun;
    private RadioGroup radio;
    private ArrayList<Beans.DataBean> data = new ArrayList<>();
    private ArrayList<ImageView> imageViews;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_frag_03,container,false);

        lun = view.findViewById(R.id.pager_lun);
        radio = view.findViewById(R.id.radio_button);

        getJson();
        return view;
    }
//--------------------------------------网络解析数据  获取图片-------------------------------------
    private void getJson() {
        new AsyncTask<String, Integer, List<Beans.DataBean>>() {
            @Override
            protected List<Beans.DataBean> doInBackground(String... strings) {
                try {
                    URL url = new URL(strings[0]);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.setRequestMethod("GET");
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    if(urlConnection.getResponseCode()==200){
                        InputStream inputStream = urlConnection.getInputStream();
                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                        while ((len=inputStream.read(bytes))!=-1){
                            outputStream.write(bytes,0,len);
                        }
                        String json = outputStream.toString();
                        inputStream.close();
                        outputStream.close();
                        Gson gson = new Gson();
                        Beans beans = gson.fromJson(json, Beans.class);

                        return beans.getData();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
//---------------------------------将解析帅的图片添加到一个集合------------------------------
            @Override
            protected void onPostExecute(List<Beans.DataBean> dataBeans) {
                super.onPostExecute(dataBeans);
                //新建一个集合
                ArrayList<String> strings = new ArrayList<>();
                imageViews = new ArrayList<>();
                for (int i = 0;i<4;i++){
                    String pic_url = dataBeans.get(i).getPic_url();
                    strings.add(pic_url);
                    ImageView imageView = new ImageView(getActivity());
                    ImageLoader.getInstance().displayImage(pic_url,imageView);
                    //创建图片的集合
                    imageViews.add(imageView);
                }
                //适配器
                MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
                lun.setAdapter(myPagerAdapter);
                handler.sendEmptyMessageDelayed(0,2000);
            }
        }.execute(path);
    }
//------------------------------------------handler发送消息  无限轮播--------------------------------------
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int currentItem = lun.getCurrentItem()+1;
            lun.setCurrentItem(currentItem);
            handler.sendEmptyMessageDelayed(0,2000);

        }
    };
//---------------------------------------------------适配器----------------------------------------------------
    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            position = position%imageViews.size();
            ImageView imageView = imageViews.get(position);
            container.addView(imageView);
            return imageView;
        }

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


猜你喜欢

转载自blog.csdn.net/weixin_43936560/article/details/86221065