Android BaseMVP结构 Fragment 范型 快速搭建、绑定MVP 只需继承Base类即可

好处:可以快速地创建一套Fragment,view、presenter、model的关系自动绑定,使代码简洁美观。

只需继承Base类,无需重写任何方法


先给出用例(是不是超简洁):

//
//Fragment
//

public class HomeFragment extends BaseFragment<HomePresenter> {
    @BindView(R2.id.home_test)
    TextView tv_test;

    @Override
    protected int getResource() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        tv_test.setText(requirePresenter().test());
    }
}

//
//Presenter
//

public class HomePresenter extends BasePresenter<HomeFragment, HomeModel> {
    public String test() {
        return requireModel().test();
    }
}

//
//Model
//

public class HomeModel extends BaseModel<HomePresenter> {
    public String test() {
        return "Test";
    }
}

BaseFragment:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.lang.reflect.ParameterizedType;
import java.util.Objects;

import butterknife.ButterKnife;

@SuppressWarnings("rawtypes")
public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
    private P presenter;
    private View root;

    @SuppressWarnings("unchecked")
    public BaseFragment() {
        super();
        try {
            presenter = (P) ((Class<P>) ((ParameterizedType) (Objects.requireNonNull(getClass().getGenericSuperclass()))).getActualTypeArguments()[0]).newInstance();
            presenter.setFragment(this);
        } catch (IllegalAccessException | java.lang.InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(getResource(), container, false);
        ButterKnife.bind(this, root);
        initView();
        return root;
    }

    protected P requirePresenter() {
        return presenter;
    }

    protected View requireRoot() {
        return root;
    }

    protected abstract int getResource();

    protected abstract void initView();
}

 BasePresenter:

import android.app.Activity;
import android.content.Context;

import java.lang.reflect.ParameterizedType;
import java.util.Objects;

@SuppressWarnings("rawtypes")
public abstract class BasePresenter<F extends BaseFragment, M extends BaseModel> {
    private F fragment;
    private M model;

    void setFragment(F fragment) {
        this.fragment = fragment;
    }

    @SuppressWarnings("unchecked")
    public BasePresenter() {
        try {
            model = ((Class<M>) ((ParameterizedType) (Objects.requireNonNull(getClass().getGenericSuperclass()))).getActualTypeArguments()[1]).newInstance();
            model.setPresenter(this);
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }

    protected Activity requireActivity() {
        return fragment.requireActivity();
    }

    protected Context requireContext() {
        return fragment.requireContext();
    }

    protected F requireFragment() {
        return fragment;
    }

    protected M requireModel() {
        return model;
    }
}

BaseModel:

package us.xingkong.app.ui.base.fragment;

import androidx.lifecycle.ViewModel;

@SuppressWarnings("rawtypes")
public class BaseModel<P extends BasePresenter> extends ViewModel {
    private P presenter;

    void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    protected P requirePresenter() {
        return presenter;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值