无限轮播


public class MainActivity extends AppCompatActivity {

    private ViewPager viewpager;
    private ArrayList<ImageView> imageViews = null;
    private RadioGroup radiogroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //控件
        viewpager = findViewById(R.id.viewpager);
        radiogroup = findViewById(R.id.radiogroup);
        //图片的数据
        getData();

  }

    //执行异步
    private void getData() {
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute();

    }

    //异步
    class MyAsyncTask extends AsyncTask<String,Integer,String> {


        @Override
        protected String doInBackground(String... strings) {
            String path = "http://www.xieast.com/api/news/news.php?page=1";
            try {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                if (conn.getResponseCode() == 200) {
                    InputStream inputStream = conn.getInputStream();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    bos.close();
                    inputStream.close();
                    String json = bos.toString();
                    return json;

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

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                JSONObject jsonObject = new JSONObject(s);
                JSONArray data = jsonObject.getJSONArray("data");
                imageViews=new ArrayList<>();
              for (int i=0;i<4;i++){
                  JSONObject jsonObject1 = data.getJSONObject(i);
                  String thumbnail_pic_s = jsonObject1.getString("thumbnail_pic_s");
                  //创建Imageview
                  ImageView imageView = new ImageView(MainActivity.this);
                  ImageLoader.getInstance().displayImage(thumbnail_pic_s,imageView);
                  //创建图片的集合
                 imageViews.add(imageView);
                 //创建小圆点
                  RadioButton radioButton = new RadioButton(MainActivity.this);
                  radiogroup.addView(radioButton);


              }
              //默认第一个
                radiogroup.check(radiogroup.getChildAt(0).getId());
             //  viewpager.setImagesUrl(imageViews);
                MyViewPagerAdapter adapter = new MyViewPagerAdapter();
                viewpager.setAdapter(adapter);
                handler.sendEmptyMessageDelayed(0,2000);

                viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int i, float v, int i1) {

                    }

                    @Override
                    public void onPageSelected(int i) {
                        i%=imageViews.size();
                        radiogroup.check(radiogroup.getChildAt(i).getId());
                    }

                    @Override
                    public void onPageScrollStateChanged(int i) {

                    }
                });


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


        }
    }


    //轮播适配器
    private class MyViewPagerAdapter extends PagerAdapter {
        //返回Integer长度 不断循环
        @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 %= 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);
        }
    }


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

            int currentItem = viewpager.getCurrentItem()+1;
            viewpager.setCurrentItem(currentItem);
            handler.sendEmptyMessageDelayed(0,2000);

        }
    };

}
















相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页