Android收藏集安卓面试jetpack

Jetpack mvvm 终

2020-10-15  本文已影响0人  三月四晴
MVC
image.png
MVP
image.png

优点:
1、模型与视图完全分离,我们可以修改视图而不影响模型。
2、可以更高效地使用模型,因为所有的交互都发生在一个地方——Presenter内部。
3、我们可以将一个Presenter用于多个视图,而不需要改变Presenter的逻辑。这个特性非常的有用,因为视图的变化总是比模型的变化频繁。
4、如果我们把逻辑放在Presenter中,那么我们就可以脱离用户接口来测试这些逻辑(单元测试)。
缺点:Presenter作为桥梁协调View和Model,就会导致Presenter变得很臃肿,维护比较困难。

MVVM
优点:
缺点:
public class MvvMViewmodel extends ViewModel {
    public MutableLiveData<Integer> mutableLiveData = new MutableLiveData<>();
    public MutableLiveData<State> state = new MutableLiveData<>();
    public LiveData<List<WanAndroidNewsBean.ItemDetailsBean>> listLiveData;
    private  int page =0;
    public  List<WanAndroidNewsBean.ItemDetailsBean> list = new ArrayList<>();
    public CompositeDisposable dis;


    public MvvMViewmodel() {
        dis = new CompositeDisposable();
        listLiveData = Transformations.switchMap(mutableLiveData, new Function<Integer, LiveData<List<WanAndroidNewsBean.ItemDetailsBean>>>() {
            @Override
            public LiveData<List<WanAndroidNewsBean.ItemDetailsBean>> apply(Integer input) {
                return getData(input);
            }
        });

    }
    public void getPageUpData(){
       mutableLiveData.setValue(page>0?page-1:page);
    }
    public void getPageNextData(){
        mutableLiveData.setValue(page+1);
    }

    private LiveData<List<WanAndroidNewsBean.ItemDetailsBean>> getData(int page){
        MediatorLiveData<List<WanAndroidNewsBean.ItemDetailsBean>> mediatorLiveData = new MediatorLiveData<>();
        HttpData.getRetrofit().create(PathApiService.class).getNes(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WanAndroidNewsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        state.setValue(new State(StateType.LOADING,""));
                        dis.add(d);
                    }

                    @Override
                    public void onNext(WanAndroidNewsBean wanAndroidNewsBean) {
                        state.setValue(new State(StateType.SUCCESS,""));
                        mediatorLiveData.setValue(wanAndroidNewsBean.getData().getDatas());
                    }

                    @Override
                    public void onError(Throwable e) {
                        state.setValue(new State(StateType.ERROR,e.getMessage()));
                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAGSS","onComplete");
                    }
                });
        return mediatorLiveData;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if(dis!=null){
            dis.dispose();
        }
    }
}
public class MvvmMainActivity extends BaseActivity {
    private MvvMViewmodel viewmodel;
    private ActivityMvvmMainBinding activityMvvmMainBinding;
    private RecycleAdapter recycleAdapter;
    private Dialog diglog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init() {
        diglog = MyDiglog.createLoadingDialog(this,"请求中");
        activityMvvmMainBinding= getViewDataBinding();
        viewmodel = new ViewModelProvider(this).get(MvvMViewmodel.class);
        diglog.show();
        activityMvvmMainBinding.setLifecycleOwner(this);
        viewmodel.getPageUpData();
        activityMvvmMainBinding.setViewModel(viewmodel);
        activityMvvmMainBinding.recycle.setLayoutManager(new LinearLayoutManager(this));
        recycleAdapter = new RecycleAdapter(viewmodel.list);

        activityMvvmMainBinding.recycle.setAdapter(recycleAdapter);
        viewmodel.listLiveData.observe(this, new Observer<List<WanAndroidNewsBean.ItemDetailsBean>>() {
            @Override
            public void onChanged(List<WanAndroidNewsBean.ItemDetailsBean> itemDetailsBeans) {
             viewmodel.list.clear();
             viewmodel.list.addAll(itemDetailsBeans);
             recycleAdapter.notifyDataSetChanged();
            }
        });

        viewmodel.state.observe(this, new Observer<State>() {
            @Override
            public void onChanged(State state) {
                diglog.dismiss();
                switch (state.getCode()){
                    case ERROR:
                        Toast.makeText(MvvmMainActivity.this,state.getMsg(),Toast.LENGTH_LONG).show();
                        break;
                    case LOADING:
                        diglog.show();
                        break;

                }
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_mvvm_main;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(diglog!=null){
            diglog.dismiss();
        }
        diglog= null;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="com.sanyue.jetpakcdemonew.mvvm.MvvMViewmodel" />

    </data>
<androidx.constraintlayout.widget.ConstraintLayout 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=".mvvm.MvvmMainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_width="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/page_up"
        android:layout_height="0dp"/>
    <Button
        android:id="@+id/page_up"
        android:layout_width="wrap_content"
        android:text="上一页"
        android:onClick="@{()->viewModel.getPageUpData()}"
        android:enabled="@{viewModel.mutableLiveData>0?true:false}"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/pageIndex"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/pageIndex"
        android:layout_width="wrap_content"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="18sp"
        app:layout_constraintLeft_toRightOf="@+id/page_up"
        app:layout_constraintRight_toLeftOf="@+id/page_down"
        app:layout_constraintTop_toTopOf="@+id/page_down"
        app:layout_constraintBottom_toBottomOf="@+id/page_down"
        android:text="@{String.valueOf(viewModel.mutableLiveData)}"
        android:layout_height="50dp"/>
    <Button
        android:id="@+id/page_down"
        android:layout_width="wrap_content"
        android:text="下一页"
        android:onClick="@{()->viewModel.getPageNextData()}"
        app:layout_constraintLeft_toRightOf="@id/pageIndex"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
效果图
上一篇下一篇

猜你喜欢

热点阅读