无限自动轮播加小圆点


    private PullToRefreshScrollView psv;
    private ViewPager vp;
    private GridView gv;
    private List picUrls;
    private int pageIndex=1;
    private String url="http://gank.io/api/data/Android/10/"+pageIndex;

    @SuppressLint("HandlerLeak")
    private Handler myHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //改变viewpager的页面
            int currentItem = vp.getCurrentItem();
            currentItem++;
            vp.setCurrentItem(currentItem);

            //设置选中的小圆点
            setSelectedPoint(currentItem%points.size());

            sendEmptyMessageDelayed(0,1000);

        }
    };
    private LinearLayout linPoints;
    private List  points=new ArrayList<>();
    private int pageType=1;
    private List data=new ArrayList<>();
    private MyGvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        psv = findViewById(R.id.psv);
        vp = findViewById(R.id.vp);
        gv = findViewById(R.id.gv);
        linPoints = findViewById(R.id.lin);

        initData();

        //初使化
        initPsv();

        //初使化小圆点
        initPoints();

        //实现轮播图
        vp.setAdapter(new MyPagerAdapter(this,picUrls));
        //自动播
        myHandler.sendEmptyMessageDelayed(0,1000);

        //连接网络请求数据
        requestNetData();




    }

    private void initPsv() {
        //1.设置模式
        psv.setMode(PullToRefreshBase.Mode.BOTH);
        //2.添加监听事件
        psv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
                //刷新
                pageType=1;
                pageIndex=1;
                url="http://gank.io/api/data/Android/10/"+pageIndex;
                requestNetData();


            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
                //加载更多
                pageType=2;
                pageIndex++;
                url="http://gank.io/api/data/Android/10/"+pageIndex;
                requestNetData();
            }
        });
    }

    private String toJson(InputStream inputStream) {
        StringBuilder builder = new StringBuilder();
        String string;
        InputStreamReader in = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(in);
        try {
            while ((string=reader.readLine())!=null){
                builder.append(string);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

    private void requestNetData() {
        new MyTask().execute(url);

    }
    //定义一个MyTask
    class MyTask extends AsyncTask {

        @Override
        protected String doInBackground(String... strings) {
            String str="";
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(5000);
                urlConnection.setReadTimeout(5000);
                int responseCode = urlConnection.getResponseCode();
                if (responseCode==200){
                    InputStream inputStream = urlConnection.getInputStream();
                    str=toJson(inputStream);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            RequestData requestData = gson.fromJson(s, RequestData.class);
            List results = requestData.getResults();
            if (pageType==1){
                data.clear();
            }
            data.addAll(results);
            setgvadapter();
            //关闭头尾布局
            psv.onRefreshComplete();

        }
    }

    private void setgvadapter() {
        if(adapter==null){
            adapter=new MyGvAdapter(this,data);
            gv.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }


    /**
     *
     * @param pageIndex 当前页面的下标
     */
    private void setSelectedPoint(int pageIndex){
        for (int i = 0; i             if(i==pageIndex){
                points.get(i).setImageResource(R.drawable.point_selected);
            }else {
                points.get(i).setImageResource(R.drawable.point_mormal);
            }

        }
    }

    private void initPoints() {
        //向linelayout中添加ImageView
        //addView()
        for (int i = 0; i             ImageView imageView=new ImageView(this);
            if(i==0){
                imageView.setImageResource(R.drawable.point_selected);//选中时的图片
            }else {
                imageView.setImageResource(R.drawable.point_mormal);
            }
            linPoints.addView(imageView);

            //添加到集合
            points.add(imageView);
        }

    }

    private void initData() {
        picUrls=new ArrayList<>();
        picUrls.add("http://img3.utuku.china.com/480x0/news/20170331/5228a6f9-5b0a-431a-b7b6-1d712ab7c86d.jpg");
        picUrls.add("http://s16.sinaimg.cn/mw690/001pPXi2gy6W1BdDgujcf&690");
        picUrls.add("http://img1.gtimg.com/ent/pics/hv1/213/155/1650/107330988.jpg");
        picUrls.add("http://mat1.gtimg.com/ent/109a/64004.jpg");

    }

你可能感兴趣的:(无限自动轮播加小圆点)