使用Banner实现图片轮播

在build.gradle导入依赖

implementation 'com.youth.banner:banner:1.4.10'

在XML中的布局






适配器中的代码

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List list;
    private final int TYPECOUNT=2;
    private final int BANNER=0;
    private final int NEWS=1;




    public MyAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setList(List list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    public void addList(List list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size()+1;
    }

    @Override
    public NewsBean.Data getItem(int position) {
        return list.get(position-1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return TYPECOUNT;
    }

    @Override
    public int getItemViewType(int position) {
        return position==0?BANNER:NEWS;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1;
       if(position==BANNER){
           if(convertView==null){
               convertView=LayoutInflater.from(context).inflate(R.layout.banneritem,parent,false);
               holder1=new ViewHolder1();
               holder1.banner=convertView.findViewById(R.id.banner);
               convertView.setTag(holder1);
           }else {
               holder1= (ViewHolder1) convertView.getTag();

           }
           holder1.getData(list,context);

       }else{
           ViewHolder holder;
           if(convertView==null){
               if(convertView==null){
                   convertView=LayoutInflater.from(context).inflate(R.layout.newsitem,parent,false);
                   holder=new ViewHolder();
                   holder.imageView=convertView.findViewById(R.id.imageView);
                   holder.textView_title=convertView.findViewById(R.id.textView_title);
                   holder.textView_sum=convertView.findViewById(R.id.textView_sum);
                   convertView.setTag(holder);
               }else {
                   holder= (ViewHolder) convertView.getTag();
               }
               holder.textView_title.setText(getItem(position).getNews_title());
               holder.textView_sum.setText(getItem(position).getNews_summary());
               ImageLoader.getInstance().displayImage(getItem(position).getPic_url(),holder.imageView);
           }

       }
        return convertView;
    }
    class ViewHolder{
        TextView textView_title,textView_sum;
        ImageView imageView;


    }
    class ViewHolder1{
        Banner banner;
        public void getData(List list,Context context){
            //设置样式
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
            //设置图片
            banner.setImageLoader(new ImageLoaderInterface() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    NewsBean.Data bean= (NewsBean.Data) path;
                    ImageLoader.getInstance().displayImage(bean.getPic_url(),imageView);
                }

                @Override
                public ImageView createImageView(Context context) {
                    ImageView imageView=new ImageView(context);
                    imageView.setScaleType(ImageView.ScaleType.CENTER);
                    return imageView;
                }

            });
            getTitle(list);

        }

        private void getTitle(List list) {
            banner.setImages(list);
            banner.setBannerTitles(getinfo(list));
            banner.setDelayTime(2000);
            banner.start();
        }

        private List getinfo(List list) {
            List list1=new ArrayList<>();
            for (int i = 0; i 

Banner在activity中的实现

public class SuccessActivity extends BaseActivity{


    private WebView webView;
    private Banner banner;
    private String path="http://www.xieast.com/api/banner.php";

    @Override
    protected void initData() {
        Intent intent1 = getIntent();
        String url = intent1.getStringExtra("url");
        webView.loadUrl(url);
        getData();

    }

    private void getData() {
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setImageLoader(new ImageLoaderInterface() {

            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                BannerBean.Data bean=(BannerBean.Data)path;
                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(bean.getImg(),imageView);
            }

            @Override
            public ImageView createImageView(Context context) {
                ImageView imageView=new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                return imageView;
            }
        });
        getTitleInfo();
    }

    private void getTitleInfo() {
       Method.getInstance().getRequest(path, BannerBean.class, new Method.CallBack() {
           @Override
           public void onSuccess(BannerBean bean) {
               List data = bean.getData();
               banner.setImages(data);
               banner.setBannerTitles(getTm(data));
               banner.setDelayTime(2000);
               banner.start();


           }
       });

    }
    public List getTm(List list){
        List list1=new ArrayList<>();
        for (int i=0;i

你可能感兴趣的:(轮播图,Banner)