mosby mvp 库的使用的使用

在app下的build.gradle 中添加

implementation 'com.hannesdorfmann.mosby3:mvp:3.1.1'

1.view

package com.example.mosbydemo;

/**
 * Created by Yangzb on 2019/4/29 14:24
 * E-mail:yangzongbin@si-top.com
 * Describe:
 */
public interface HelloWorldView extends MvpView{
    void showHello(String greetingText);

    void showGoodbye(String greetingText);
}

2.presenter

package com.example.mosbydemo;


import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;

/**
 * Created by Yangzb on 2019/4/29 14:25
 * E-mail:yangzongbin@si-top.com
 * Describe:
 */
public class HelloWorldPresenter extends MvpBasePresenter<HelloWorldView> {
    private GreetingGeneratorTask greetingTask;
    private void cancelGreetingTaskIfRunning(){
        if (greetingTask != null){
            greetingTask.cancel(true);
        }
    }
    public void greetHello(){
        cancelGreetingTaskIfRunning();

        greetingTask = new GreetingGeneratorTask("Hello", new GreetingGeneratorTask.GreetingTaskListener() {
            @Override
            public void onGreetingGenerated(String greetingText) {
                if (isViewAttached()) {
                    getView().showHello(greetingText);
                }
            }
        });
        greetingTask.execute();
    }
    public void greetGoodbye(){
        cancelGreetingTaskIfRunning();

        greetingTask = new GreetingGeneratorTask("Goodbye", new GreetingGeneratorTask.GreetingTaskListener() {
            @Override
            public void onGreetingGenerated(String greetingText) {
                if (isViewAttached()){
                    getView().showGoodbye(greetingText);
                }
            }
        });
        greetingTask.execute();
    }
}

3.activity

package com.example.mosbydemo;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends MvpActivity<HelloWorldView,HelloWorldPresenter> implements HelloWorldView {

    private TextView greetingTextView;
    private Button hello;
    private Button goodbye;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        greetingTextView = findViewById(R.id.id_tv);
        hello = findViewById(R.id.id_btn_hello);
        goodbye = findViewById(R.id.id_btn_goodbye);

        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.greetHello();
            }
        });
        goodbye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.greetGoodbye();
            }
        });
    }

    @NonNull
    @Override
    public HelloWorldPresenter createPresenter() {
        return new HelloWorldPresenter();
    }

    @Override
    public void showHello(String greetingText) {
        greetingTextView.setTextColor(Color.RED);
        greetingTextView.setText(greetingText);
    }

    @Override
    public void showGoodbye(String greetingText) {
        greetingTextView.setTextColor(Color.BLUE);
        greetingTextView.setText(greetingText);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void closeLoading() {

    }

    @Override
    public void showToast(String msg) {

    }
}

4.layout

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/id_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:id="@+id/id_btn_hello"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Goodbye"
        android:id="@+id/id_btn_goodbye"/>
</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值