博客
关于我
Android MVP实现接口Banner轮播图
阅读量:657 次
发布时间:2019-03-15

本文共 3809 字,大约阅读时间需要 12 分钟。

Mainactivity

package com.example.lunbo;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.Toast;import com.bumptech.glide.Glide;import com.example.lunbo.bean.Result;import com.example.lunbo.bean.User;import com.example.lunbo.core.CallBacks;import com.example.lunbo.presenter.LunPresenter;import com.youth.banner.Banner;import com.youth.banner.loader.ImageLoader;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements CallBacks
>{ String url = "http://www.zhaoapi.cn/ad/getAd"; List
string = new ArrayList<>(); List
muser = new ArrayList<>(); private Banner banner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 banner = findViewById(R.id.banner); //创建presenter LunPresenter lunPresenter = new LunPresenter(this); lunPresenter.add(url); } //成功 @Override public void success(List
data) { Toast.makeText(this, ""+data.toString(), Toast.LENGTH_SHORT).show(); muser.addAll(data); //轮播图方法 mBanners(); } //失败 @Override public void fail(Result result) { Toast.makeText(this, ""+result.getMsg(), Toast.LENGTH_SHORT).show(); } //轮播图 private void mBanners() { banner.setImageLoader(new ImageLoader() { @Override public void displayImage(Context context, Object path, ImageView imageView) { // 使用Glide 加载图片 Glide.with(MainActivity.this).load(path).into(imageView); } }); // for循环 加载图片 for (int i = 0; i < muser.size(); i++) { String icon = muser.get(i).getIcon(); string.add(icon); } banner.setImages(string); // 设置 banner.start(); // 开始 }}

布局

Presenter层

package com.example.lunbo.presenter;import android.os.Handler;import android.os.Looper;import android.os.Message;import com.example.lunbo.MainActivity;import com.example.lunbo.bean.Result;import com.example.lunbo.core.CallBacks;import com.example.lunbo.model.LunModel;public class LunPresenter {    CallBacks callBacks;    public LunPresenter(CallBacks callBacks) {        this.callBacks=callBacks;    }    Handler handler = new Handler(Looper.getMainLooper()){        @Override        public void handleMessage(Message msg) {            Result result = (Result) msg.obj;            if (result.getCode()==0){                callBacks.success(result.getData());            }else {                callBacks.fail(result);            }        }    };    public void add(final String url) {        new Thread(new Runnable() {            @Override            public void run() {                Result result = LunModel.lun(url);                Message message = handler.obtainMessage();                message.obj=result;                handler.sendMessage(message);            }        }).start();    }}

Model

package com.example.lunbo.model;import com.example.lunbo.bean.Result;import com.example.lunbo.bean.User;import com.example.lunbo.utils.OkHttp;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;import java.util.List;public class LunModel {    public static Result lun(String url){        String s = OkHttp.get(url);        Gson gson = new Gson();        Type type = new TypeToken
>>() {}.getType(); Result result = gson.fromJson(s, type); return result; }}

依赖

compile 'com.youth.banner:banner:1.4.9'

权限

转载地址:http://khfmz.baihongyu.com/

你可能感兴趣的文章
Mysql 常见错误
查看>>
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>