框架【库】程序编码UI

Android AAC模式,该与 MVP 分手了!

2019-01-04  本文已影响48人  Linhaojian
Jetpack.jpg

1.前言


2.简介


3.结构关系

aac模式.png
  1. 负责处理更新UI逻辑;
  2. (可选)负责绑定UI & 通知UI更新;
  3. 负责管理数据 & 处理生命周期;
  4. 负责在数据更改时,通知视图;
  5. 负责获取不同来源的数据 | 处理业务数据逻辑;

4.实例

4.1 Base

/**
 * @author Created by LHJ on 2018/12/13 14:48.
 * @description BaseActivity基础类,处理ViewModelProvider的初始化
 */
public abstract class BaseActivity extends AppCompatActivity {
    private ViewModelProvider viewModelProvider;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        viewModelProvider = getViewModelProvider();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModelProvider = null;
    }
    /**
     * 创建ViewModel对象
     * @param clazz
     * @return
     */
    public <T extends ViewModel> T get(Class<T> clazz){
        return viewModelProvider.get(clazz);
    }
    /**
     * 初始化ViewModelProvider对象
     * @return
     */
    private ViewModelProvider getViewModelProvider(){
        return ViewModelProviders.of(this);
    }
    /**
     * 获取布局ID
     * @return
     */
    protected abstract int getLayoutId();
}
/**
 * @author Created by LHJ on 2018/12/13 14:48.
 * @description BaseActivity基础类,处理ViewDataBinding的初始化
 */
public abstract class BaseDataBindActivity<V extends ViewDataBinding> extends BaseActivity {
    protected V dataBind;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBind = DataBindingUtil.setContentView(this,getLayoutId());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(dataBind!=null){
            dataBind.unbind();
            dataBind = null;
        }
    }
    /**
     * 获取布局ID
     * @return
     */
    protected abstract int getLayoutId();
}
/**
 * @author Created by LHJ on 2019/1/4 11:21.
 * @description  BaseFragment,处理ViewModelProvider的初始化
 */
public class BaseFragment extends Fragment {
    private ViewModelProvider viewModelProvider;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModelProvider = getViewModelProvider();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModelProvider = null;
    }
    /**
     * 创建ViewModel对象
     * @param clazz
     * @return
     */
    public <T extends ViewModel> T get(Class<T> clazz){
        return viewModelProvider.get(clazz);
    }
    /**
     * 初始化ViewModelProvider对象
     * @return
     */
    private ViewModelProvider getViewModelProvider(){
        return ViewModelProviders.of(this);
    }
}
/**
 * @author Created by LHJ on 2018/12/13 14:52.
 * @description 基础ViewModel类,管理LiveData
 */
public class BaseViewModel extends ViewModel {
    private Map<String,MutableLiveData> maps;
    /**
     * 构造函数(在ViewModelProvider里通过class.newInstance创建实例)
     */
    public BaseViewModel(){
        //初始化集合(线程安全)
        maps = new ConcurrentHashMap<>();
    }
    /**
     * 通过指定的数据实体类获取对应的MutableLiveData类
     * @param clazz
     * @param <T>
     * @return
     */
    protected <T> MutableLiveData<T> get(Class<T> clazz){
        return get(null,clazz);
    }
    /**
     *  通过指定的key或者数据实体类获取对应的MutableLiveData类
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    protected <T> MutableLiveData<T> get(String key,Class<T> clazz){
        String keyName = "";
        if(TextUtils.isEmpty(key)){
            keyName = clazz.getCanonicalName();
        }else{
            keyName = key;
        }
        MutableLiveData<T> mutableLiveData = maps.get(keyName);
        //1.判断集合是否已经存在livedata对象
        if(mutableLiveData!=null){
            return mutableLiveData;
        }
        //2.如果集合中没有对应实体类的Livedata对象,就创建并添加至集合中
        mutableLiveData = new MutableLiveData<>();
        maps.put(keyName,mutableLiveData);
        return mutableLiveData;
    }
    /**
     * 在对应的FragmentActivity销毁之后调用
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void onCleared() {
        super.onCleared();
        if(maps!=null){
            maps.clear();
        }
    }
}

4.2 使用

/**
 * @author Created by LHJ on 2018/12/13 15:31.
 * @description 管理数据
 */
public class AViewModel extends BaseViewModel {
    public MutableLiveData<User> getUserMutableLiveData() {return get(User.class);}
    public MutableLiveData<Person> getPersonMutableLiveData(){return get(Person.class);}
}
public class ActivityA extends BaseActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1.添加数据更改监听器
        get(AViewModel.class).getUserMutableLiveData().observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable User user) {
                Log.e("linhaojian","ActivityA中接收user:"+user.toString());
            }
        });
    }
    @Override
    protected int getLayoutId() {
        return R.layout.a1;
    }
    // 2.更改数据
    public void getUser(){
        User user = new User();
        user.setName("lin");
        user.setPw("123456");
        user.setSex("male");
        //同步更改setValue  ;  异步更改postValue
        get(AViewModel.class).getUserMutableLiveData().setValue(user);
    }
}

5.总结

欢迎关注linhaojian_CSDN博客或者linhaojian_简书

不定期分享关于安卓开发的干货。


写技术文章初心

  • 技术知识积累
  • 技术知识巩固
  • 技术知识分享
  • 技术知识交流
上一篇下一篇

猜你喜欢

热点阅读