欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中一个MVP架构的简单Demo

程序员文章站 2022-07-01 16:23:20
三个接口(首先用接口定义好各层要做的事),三个具体的实现IPresenterpackage com.zhangyu.mymvp;public interface IPresenter { //查询 void search(String query);}IUserServicepackage com.zhangyu.mymvp;public interface IUserService { String searchUser(int id);}IVie.....

Android 中一个MVP架构的简单Demo
Android 中一个MVP架构的简单Demo

三个接口(首先用接口定义好各层要做的事),三个具体的实现

IPresenter

package com.zhangyu.mymvp;

public interface IPresenter {
    //查询
    void search(String query);
}

IUserService

package com.zhangyu.mymvp;

public interface IUserService {
    String searchUser(int id);
}

IView

package com.zhangyu.mymvp;

//需要对UI进行的操作
public interface IView {
    //展示结果
    void showResult(String result);
}

MainActivity持有IPresenter

现在的activity是单纯的view层,只需要负责UI的更新,业务逻辑等放到Presenter中去处理

package com.zhangyu.mymvp;

import android.os.Bundle;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements IView {

    private SearchView svSearch;
    private TextView tvResult;

    private IPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        presenter = new Presenter(this);
    }

    @Override
    public void showResult(String result) {
        tvResult.setText(result);
    }

    private void initView() {
        svSearch = (SearchView) findViewById(R.id.sv_search);
        tvResult = (TextView) findViewById(R.id.tv_result);
        //展开搜索框
        svSearch.onActionViewExpanded();
        svSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(MainActivity.this, "开始搜索onQueryTextSubmit", Toast.LENGTH_SHORT).show();
                presenter.search(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }
}

Presenter持有IView、IUserService

Presenter中主要处理业务逻辑,对于MVC模式中的Controller层中抽离出业务逻辑层。

package com.zhangyu.mymvp;

public class Presenter implements IPresenter {

    private IView iView;
    private IUserService iUserService;

    public Presenter(IView iView) {
        this.iView = iView;
        iUserService = new UserService();
    }

    @Override
    public void search(String query) {
        int id = Integer.parseInt(query);
        String result = iUserService.searchUser(id);
        iView.showResult(result);
    }
}

UserService

具体的网络请求部分,这里只是做一个模拟

package com.zhangyu.mymvp;

public class UserService implements IUserService {

    @Override
    public String searchUser(int id) {
        return "查询到的结果——" + id + ":zhangyu";
    }
}

本文地址:https://blog.csdn.net/yu540135101/article/details/108584894

相关标签: Android基础