1. 程式人生 > >輪播圖片

輪播圖片

private MyHandler myHandler = new MyHandler();

  Gson gson=new Gson();
            PicBean picBean = gson.fromJson(json, PicBean.class);
            List<PicBean.DataBean> list = picBean.getData();
            List<ImageView> imageViews=new ArrayList<>();
            for (int i = 0; i <list.size() ; i++) {
                String url = list.get(i).getImg();
                ImageView imageView = new ImageView(MyApp.getContext());
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ImageLoader.getInstance().displayImage(url,imageView,MyApp.getOptions());
                imageViews.add(imageView);
            }
            PicAdapter adapter = new PicAdapter(imageViews);
            f11_pic_view.setAdapter(adapter);
            //                開始進行自動輪播
            myHandler.sendEmptyMessageDelayed(0, 1000);

// handler
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
int currentItem = viewPager.getCurrentItem();
currentItem++;
viewPager.setCurrentItem(currentItem);
myHandler.sendEmptyMessageDelayed(0, 1000);
}
}

// 頁面銷燬的時候,停止handler傳送訊息
@Override
public void onPause() {
super.onPause();
myHandler.removeCallbacksAndMessages(null);
}