mosby mvp 库的使用的使用
程序员文章站
2022-05-09 13:50:13
...
在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:[email protected]
* 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:[email protected]
* 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>
上一篇: week07_day03_Tree
下一篇: week06_day01