Android MVVM-编程思想3(封装基类BaseMvvmActivity,BaseMvvmFragment)

Posted 0 and 1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android MVVM-编程思想3(封装基类BaseMvvmActivity,BaseMvvmFragment)相关的知识,希望对你有一定的参考价值。

前言

通过一个小案例,帮助大家了解MVVM。最终实现一个MVVM通用框架。代码:github。(如有错误之处,请在评论区指出,谢谢。如果感觉写的不错,请点赞,关注,谢谢。)

上一个小节,只是理论介绍了MVVM,DataBinding,ViewModel,LiveData,这一小节,讲解一下他们的使用方式。如果已经对这些基础知识很了解,可以直接看下一节。

目录:

Android MVVM-编程思想1(入门介绍MVVM,DataBinding,ViewModel,LiveData)
Android MVVM-编程思想2(入门实战MVVM,DataBinding,ViewModel,LiveData)
Android MVVM-编程思想3(封装基类BaseMvvmActivity,BaseMvvmFragment)

上一小节讲解了DataBinding,ViewModel,LiveData 的入门使用,这一节把这些初始化,通用设置封装到基类中。

分析UserActivity可以抽取的逻辑有哪些?

Activity 需要持有ViewDataBinding,和ViewModel的引用,这两个声明和初始化过程可以抽取到基类。

public class UserActivity extends AppCompatActivity 
    private ActivityUserBinding binding;
    private UserVM userVM;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user);
        binding.setTitle("DataBinding绑定演示");
        userVM = ViewModelProviders.of(this).get(UserVM.class); //结合ViewModel
        binding.setLifecycleOwner(this);
        binding.setUserVM(userVM);
        binding.mButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                userVM.getUser();
            
        );
    


抽取到基类之后的代码:BaseMvvmActivity

public abstract class BaseMvvmActivity<VM extends ViewModel, VDB extends ViewDataBinding> extends AppCompatActivity 
    protected VM mViewModel;
    protected VDB mViewDataBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mViewDataBind = DataBindingUtil.setContentView(this, getLayoutId());
        mViewDataBind.setLifecycleOwner(this);
        //获得泛型参数的实际类型
        Class<VM> vmClass = (Class<VM>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        mViewModel = ViewModelProviders.of(this).get(vmClass);

        afterCreate();
    


    protected abstract @LayoutRes
    int getLayoutId();

    protected abstract void afterCreate();



如何使用BaseMvvmActivity?

public class UserActivity extends BaseMvvmActivity<UserVM, ActivityUserBinding> 


    @Override
    protected int getLayoutId() 
        return R.layout.activity_user;
    

    @Override
    protected void afterCreate() 
        mViewDataBind.setTitle("DataBinding绑定演示");
        mViewDataBind.setUserVM(mViewModel);
        mViewDataBind.mButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                mViewModel.getUser();
            
        );
    

是不是简单了一些。

抽取基类BaseMvvmFragment

public abstract class BaseMvvmFragment<VM extends ViewModel, VDB extends ViewDataBinding> extends Fragment 
    protected VM mViewModel;
    protected VDB mViewDataBind;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) 
        mViewDataBind = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        mViewDataBind.setLifecycleOwner(this);
        //获得泛型参数的实际类型
        Class<VM> vmClass = (Class<VM>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        mViewModel = ViewModelProviders.of(this).get(vmClass);
        return mViewDataBind.getRoot();
    


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);
        afterCreate();
    

    protected abstract @LayoutRes
    int getLayoutId();

    protected abstract void afterCreate();

基本的封装已经讲完了,后面有时间,我会把Rxjava集成进来。

以上是关于Android MVVM-编程思想3(封装基类BaseMvvmActivity,BaseMvvmFragment)的主要内容,如果未能解决你的问题,请参考以下文章

Android MVP-编程思想3(MVP-内存泄露问题处理,基类封装,有没有必要再使用软引用?)

Android MVVM-编程思想2(入门实战MVVM,DataBinding,ViewModel,LiveData)

Android MVVM-编程思想1(入门介绍MVVM,DataBinding,ViewModel,LiveData)

Android MVVM-编程思想1(入门介绍MVVM,DataBinding,ViewModel,LiveData)

Android MVVM-编程思想2(入门实战MVVM,DataBinding,ViewModel,LiveData)

Android MVP-编程思想1(什么是MVC-MVP-MVVM模式?)