无线轮播+小圆点

package com.example.manhua_demo.com.bawei.fragment;

import android.content.Intent;
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.app.FragmentTransaction;
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.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.manhua_demo.ListFindActivity;
import com.example.manhua_demo.R;
import com.example.manhua_demo.com.bawei.Bean.ListBean;
import com.example.manhua_demo.com.bawei.Bean.ManBean;
import com.example.manhua_demo.com.bawei.util.NewWorkGson;
import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by C-PC on 2017/8/18.
 */
public class Fragment01 extends Fragment {
    private int[] image = {R.mipmap.vp1, R.mipmap.vp2, R.mipmap.vp3, R.mipmap.vp4,};
    private int[] image2 = {R.mipmap.shaonian, R.mipmap.qingnian, R.mipmap.shaonv, R.mipmap.danmei};
    private ViewPager vp_title;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.notifyDataSetChanged();
            int position = vp_title.getCurrentItem();
            //当前索引+1
            position++;
            vp_title.setCurrentItem(position);
            senMsg();
        }
    };
    private TitleAdapter adapter;
    private ArrayList imagelist;
    private LinearLayout ll;
    private GridView gv;
    private ByteArrayOutputStream baos;
    private List result;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01, null);
        vp_title = (ViewPager) view.findViewById(R.id.vp_title);
        ll = (LinearLayout) view.findViewById(R.id.ll);
        gv = (GridView) view.findViewById(R.id.gv);

        adapter = new TitleAdapter();
        vp_title.setAdapter(adapter);
        //初始化小点点
        initDot();
        senMsg();
        vp_title.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int x = 0; x < imagelist.size(); x++) {

                    if (position % imagelist.size() == x) {
                        //证明点和图片是对应的,应该点是变红的
                        imagelist.get(position % imagelist.size()).setImageResource(R.drawable.dot_focuable);
                    } else {
                        imagelist.get(x).setImageResource(R.drawable.dot_normal);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //GridView
        new Thread() {
            @Override
            public void run() {
                super.run();
                //String path = "http://japi.juhe.cn/comic/category?key=2b0bbf76b0546245c32328ca0acd43d6";
                String path="http://japi.juhe.cn/comic/category?key=fd6d4ee012e09d512c19869131118f02";
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(3000);
                    int responseCode = connection.getResponseCode();
                    baos = new ByteArrayOutputStream();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        byte[] arr = new byte[1024];
                        int len;
                        while ((len = inputStream.read(arr)) != -1) {
                            baos.write(arr, 0, len);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        ManBean manBean = gson.fromJson(baos.toString(), ManBean.class);
                        //Toast.makeText(getActivity(),manBean.toString(),Toast.LENGTH_SHORT).show();
                        result = manBean.getResult();
                        gv.setAdapter(new GridAdapter());
                    }
                });
            }
        }.start();

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ListFindActivity.class);
                String name = result.get(position);
                intent.putExtra("name",name);
                startActivity(intent);
            }
        });


        return view;
    }

    //初始化小点点
    private void initDot() {
        imagelist = new ArrayList<>();
        if (imagelist != null) {
            imagelist.clear();
        }
        for (int x = 0; x < image.length; x++) {
            ImageView iv = new ImageView(getActivity());
            if (x == 0) {
                //第一张,让原点进行选中的状态
                iv.setImageResource(R.drawable.dot_focuable);
            } else {
                iv.setImageResource(R.drawable.dot_normal);
                //其他的都不选中
            }
            //设置点的宽高
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(5, 5);
            params.setMargins(10, 0, 10, 0);
            ll.addView(iv, params);
            imagelist.add(iv);
        }
    }

    class TitleAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = new ImageView(getActivity());
            //防止下标越界
            iv.setImageResource(image[position % image.length]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(iv);
            return iv;
        }

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

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

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

    //无线轮播
    public void senMsg() {
        handler.sendEmptyMessageDelayed(1, 2000);
    }

    class GridAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return result.size();
        }

        @Override
        public Object getItem(int position) {
            return result.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            viewHolder holder;
            if (convertView == null) {
                holder = new viewHolder();
                convertView = View.inflate(getActivity(), R.layout.grid_item, null);
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_grid);
                holder.tv = (TextView) convertView.findViewById(R.id.tv_grid);
                convertView.setTag(holder);
            } else {
                holder = (viewHolder) convertView.getTag();
            }
            String bean = result.get(position);
            holder.iv.setImageResource(image2[position]);
            holder.tv.setText(result.get(position));
            return convertView;
        }

        class viewHolder {
            ImageView iv;
            TextView tv;
        }
    }
}

你可能感兴趣的:(无线轮播+小圆点)