Banner的简单实用

1,.在使用Banner的时候需要添加依赖

compile 'com.youth.banner:banner:1.4.9'
2.还要初始化ImageLoader的 配置

3.创建MImageLoader

public class MImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(path.toString(),imageView);
    }
}

4.代码展示

public class MainActivity extends AppCompatActivity {
    private List<String> list;
    private Banner banner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<>();
        new MHttpURLConnectionAsyncTask().execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0");




    }

        //网络获取数据
        class MHttpURLConnectionAsyncTask extends AsyncTask<String,Void,String> {

            @Override
            protected String doInBackground(String... strings) {
                try {
                    URL url = new URL(strings[0]);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = urlConnection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer buffer = new StringBuffer();
                        String string = "";
                        while ((string = reader.readLine()) != null) {
                            buffer.append(string);
                        }
                        return buffer.toString();

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
                List<JsonBean.DataScrollBean> dataScroll = jsonBean.getDataScroll();
                for(int i=0;i<dataScroll.size();i++){
                    list.add("http://www.93.gov.cn/"+dataScroll.get(i).getIMAGEURL());
                    Log.e("AAAAAAAa",list.get(i));
                }
                banner = (Banner) findViewById(R.id.banner);
                //设置banner样式
                // banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
                //设置图片加载器
                banner.setImageLoader(new MImageLoader());
                //设置图片集合
                banner.setImages(list);
                //设置banner动画效果
                //banner.setBannerAnimation(Transformer.RotateDown);
                //设置标题集合(当banner样式有显示title时)
                //banner.setBannerTitles(imageTitle);
                //设置轮播时间
                banner.setDelayTime(3000);
                //设置指示器位置(当banner模式中有指示器时)
                //banner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                banner.start();




            }
        }
}

你可能感兴趣的:(android)