1. 程式人生 > >網路無限輪播圖

網路無限輪播圖

首先執行步驟如下

1.viewPager需要無限輪播
2.小圓點的個數
3.imageLoader載入圖片
4.handler自動輪播
一.在佈局檔案上
<android.support.v4.view.ViewPager android:layout_height="200dp" android:layout_width="match_parent" android:id="@+id/view_pager"> </android.support.v4.view.ViewPager>

<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linear_layout" android:orientation="horizontal" android:layout_marginBottom="20dp" android:layout_centerHorizontal="true" android:layout_alignBottom="@+id/view_pager"> </LinearLayout>

main主函式
public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<String> list;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                int currentItem = viewPager.getCurrentItem();
                viewPager.setCurrentItem(currentItem+1);

                handler.sendEmptyMessageDelayed(0,2000);
            }
        }
    };
    private LinearLayout linearLayout;
    private List<ImageView> images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewPager = (ViewPager) findViewById(R.id.view_pager);
        linearLayout = (LinearLayout) findViewById(R.id.linear_layout);

        //資料.....裝的是path路徑
        list = new ArrayList<>();
       在list裡面進行新增圖片路徑
        //初始化小圓點
        initDoc();

        //設定介面卡
        ImagePager imagePager = new ImagePager(MainActivity.this, list,handler);
        //
        viewPager.setAdapter(imagePager);

        //設定ViewPager初始展示的位置
        viewPager.setCurrentItem(list.size()*10000);


        //傳送延時訊息
        handler.sendEmptyMessageDelayed(0,2000);


        //viewPager頁面改變的監聽事件
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //如果選中了這一頁,,,當前小圓點紅色,,,否則綠色
                for (int i = 0;i<images.size();i++){
                    if (i== position%images.size()){
                        images.get(i).setImageResource(R.drawable.doc_select);
                    }else {
                        images.get(i).setImageResource(R.drawable.doc_select_no);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化小圓點
     */
    private void initDoc() {
        //首先需要一個集合記錄這些小圓點的圖片,,,,當頁面切換的時候,可以從集合中取出imageView進行顯示圖片的設定
        images = new ArrayList<>();
        linearLayout.removeAllViews();//清空/移除所有的view

        for (int i = 0;i<list.size();i++){
            ImageView imageView = new ImageView(MainActivity.this);

            if (i==0){//顯示第一頁,,,紅的
                imageView.setImageResource(R.drawable.doc_select);
            }else {//綠的
                imageView.setImageResource(R.drawable.doc_select_no);
            }

            //新增到集合
            images.add(imageView);

            //加入到線性佈局中顯示
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(5,0,5,0);

            linearLayout.addView(imageView,params);

        }


    }
//介面卡建立
public class ImagePager extends PagerAdapter {
Context context;
List<String> list;
Handler handler;

public ImagePager(Context context, List<String> list, Handler handler) {
    this.context = context;
    this.list = list;
    this.handler = handler;

    //初始化
    //ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    //返回當前顯示的檢視
    ImageView imageView = new ImageView(context);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);

    //載入網路的圖片
    ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView);

    /*imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("----","點選事件執行了");
        }
    });*/

    //imageView觸控的監聽事件
    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            boolean isTiaoZhuan = true;

            switch (motionEvent.getAction()){//獲取動作
                case MotionEvent.ACTION_DOWN://按下
                    Log.i("--","ACTION_DOWN");
                    handler.removeCallbacksAndMessages(null);
                    break;
                case MotionEvent.ACTION_MOVE://移動
                    Log.i("--","ACTION_MOVE");
                    isTiaoZhuan = false;
                    handler.removeCallbacksAndMessages(null);

                    break;
                case MotionEvent.ACTION_CANCEL://取消
                    Log.i("--","ACTION_CANCEL");
                    isTiaoZhuan = false;
                    handler.sendEmptyMessageDelayed(0,2000);
                    break;
                case MotionEvent.ACTION_UP://擡起
                    Log.i("--","ACTION_UP");
                    if (isTiaoZhuan){
                        Toast.makeText(context,"跳轉頁面",Toast.LENGTH_SHORT).show();

                        /*Intent intent = new Intent();

                        context.startActivity();*/
                    }
                    handler.sendEmptyMessageDelayed(0,2000);

                    break;
            }

            //自己處理觸控事件....如果當前位置返回了true,,,點選事件將不會執行,,,表示動作自己處理,不會傳遞
            return true;
        }
    });

    //新增到容器
    container.addView(imageView);

    return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    container.removeView((View) object);
}