MVP+banner+banner点击事件+webview

                                                                                   实现banner轮播图

看一下效果图:



MVP+banner+banner点击事件+webview_第1张图片


记住要加okhttp包和拦截器,可以从网上下载,记得添加okhttp、gson、banner、gilde和imageloader依赖,我也话不多说,直接上代码;


MVP的model界面代码:

package banner.bwie.com.banner.model;

import banner.bwie.com.banner.okhttp.OkHttp3Utils;
import okhttp3.Callback;

/**
 * Created by CZ on 2017/12/2.
 */
public class BannerModel {
    public void getData(Callback callback) {
        //这是调用他的数据连接
        OkHttp3Utils.doGet("http://120.27.23.105/ad/getAd", callback);
    }
}

MVP的view界面代码:

package banner.bwie.com.banner.view;

import banner.bwie.com.banner.bean.Bean;

/**
 * Created by CZ on 2017/12/2.
 */
public interface BannerView {
    //成功
    public void success(Bean bean);
    //失败
    public void fail(Exception e);
}

MVP的persenter界面代码:

package banner.bwie.com.banner.persenter;

import android.content.Context;

import com.google.gson.Gson;

import java.io.IOException;

import banner.bwie.com.banner.bean.Bean;
import banner.bwie.com.banner.model.BannerModel;
import banner.bwie.com.banner.okhttp.OnUiCallback;
import banner.bwie.com.banner.view.BannerView;
import okhttp3.Call;

/**
 * Created by CZ on 2017/12/2.
 */
public class BannerPersenter {

    BannerView view;
    Context context;
    private final BannerModel model;

    public BannerPersenter(BannerView view, Context context) {
        this.view = view;
        this.context = context;
        model = new BannerModel();
    }

    public void get() {
        model.getData(new OnUiCallback() {
            //请求数据失败后走这步
            @Override
            public void onFailed(Call call, IOException e) {
                //判断view等于是否空
                if (view != null) {
                    view.fail(e);
                }
            }

            //成功走这步
            @Override
            public void onSuccess(String result) {
                //gson解析
                Gson gson = new Gson();
                //把解析的数据添加到bean类里
                Bean bean = gson.fromJson(result, Bean.class);
                view.success(bean);

            }
        });
    }

    //防止内存泄漏
    public void sds() {
        this.view = null;
    }
}


MainActivity 页面代码:

package banner.bwie.com.banner;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import banner.bwie.com.banner.bean.Bean;
import banner.bwie.com.banner.persenter.BannerPersenter;
import banner.bwie.com.banner.view.BannerView;

public class MainActivity extends AppCompatActivity implements BannerView {


    BannerPersenter persenter = new BannerPersenter(this, this);
    List list = new ArrayList<>();
    private Banner banner;
    private List imageArray;
    private List imageTitle;
    List bean = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner = (Banner) findViewById(R.id.banner);
        persenter.get();

    }


    @Override
    public void success(final Bean bean) {
        //往空的数组添加图片
        imageArray = new ArrayList<>();
        imageArray.add(bean.getData().get(0).getIcon());
        imageArray.add(bean.getData().get(1).getIcon());
        imageArray.add(bean.getData().get(2).getIcon());
        imageArray.add(bean.getData().get(3).getIcon());
        //往空的数组添加标题
        imageTitle = new ArrayList<>();
        imageTitle.add(bean.getData().get(0).getTitle());
        imageTitle.add(bean.getData().get(1).getTitle());
        imageTitle.add(bean.getData().get(2).getTitle());
        imageTitle.add(bean.getData().get(3).getTitle());
        //拿到的成员变量
        banner = (Banner) findViewById(R.id.banner);
        //设置banner样式    这个也可以换了直接点CIRCLE_INDICATOR就可以不用添加标题和动画了
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imageArray);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.RotateDown);
        //设置标题集合(当banner样式有显示title时)
        banner.setBannerTitles(imageTitle);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        //banner点击事件
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(MainActivity.this, "我要跳转到商品详情页", Toast.LENGTH_SHORT).show();
                //循环拿出type
                for (int i = 0; i < bean.getData().size(); i++) {
                    //判断type是0还是1
                    if (bean.getData().get(i).getType() == 0) {
                        //是0的话进行跳转
                        Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
                        //通过intent传数据
                        intent.putExtra("url", bean.getData().get(i).getUrl());
                        startActivity(intent);
                        //否则是0,进行吐司
                    } else if (bean.getData().get(i).getType() == 1) {
                        Toast.makeText(MainActivity.this, "我要跳转到商品详情页", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    //这是没拿到数据的时候走的
    @Override
    public void fail(Exception e) {
        Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
    }

    //销毁事件
    @Override
    protected void onDestroy() {
        super.onDestroy();
        persenter.sds();
    }
}


GlideImageLoader页面代码
注:imageloader用banner的

package banner.bwie.com.jingdongyemian.fragment_01;


import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;

/**
 * Created by CZ on 2017/12/5.
 */
public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        Glide.with(context).load((String) path).into(imageView);
    }
}










你可能感兴趣的:(MVP+banner+banner点击事件+webview)