Android MVP实现接口Banner轮播图

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<List<User>>{
    String url = "http://www.zhaoapi.cn/ad/getAd";
    List<String> string = new ArrayList<>();
    List<User> 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<User> 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();   //  开始

    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.youth.banner.Banner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        ></com.youth.banner.Banner>
</LinearLayout>

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<Result<List<User>>>() {}.getType();
        Result result = gson.fromJson(s, type);
        return result;
    }
}

依赖

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

权限

<uses-permission android:name="android.permission.INTERNET"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值