네트워크 요청 Banner 윤 방 사용 불 러 오기

1.의지
compile 'com.youth.banner:banner:+'

2.    
compile 'com.github.bumptech.glide:glide:3.6.1'

3.       banner

4.MainActivity
public class MainFragment extends Fragment {
    private String url = "http://120.27.23.105/ad/getAd";
    private Banner banner;
    List list = new ArrayList<>();
    List images = new ArrayList<>();
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String s = (String) msg.obj;
            Gson gson = new Gson();
            MainBean mainbean = gson.fromJson(s , MainBean.class);

            for (int i = 0; i < mainbean.getData().size(); i++) {
                images.add(mainbean.getData().get(i).getIcon());
            }

            list = mainbean.getData();
            banner.setImages(images);
            banner.start();
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mainfragment ,container ,false );
        banner = (Banner) view.findViewById(R.id.banner);
        banner.setImageLoader(new GlideImageLoder());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setIndicatorGravity(Banner.TEXT_ALIGNMENT_CENTER);
        banner.setDelayTime(2000);
        jiexi();
        return view;
    }

    public class GlideImageLoder extends ImageLoader{

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }

    private void jiexi() {
        OkHttp3Utils.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Message message = new Message();
                message.obj = string;
                handler.sendMessage(message);
            }
        });
    }


}

좋은 웹페이지 즐겨찾기