Banner的简单实用

移动开发 waitig 519℃ 百度已收录 0评论

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();




            }
        }
}


本文由【waitig】发表在等英博客
本文固定链接:Banner的简单实用
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)