selfzhangtq / t-mvvm Goto Github PK
View Code? Open in Web Editor NEWThis repository contains a detailed sample app that implements MVVM architecture using LiveData,ViewModel,Retrofit,Rxjava
This repository contains a detailed sample app that implements MVVM architecture using LiveData,ViewModel,Retrofit,Rxjava
` public static class LiveBusData extends MutableLiveData {
private boolean isFirstSubscribe;
LiveBusData(boolean isFirstSubscribe) {
this.isFirstSubscribe = isFirstSubscribe;
}
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
super.observe(owner, new ObserverWrapper<>(observer, isFirstSubscribe));
}
}
private static class ObserverWrapper<T> implements Observer<T> {
private Observer<T> observer;
private boolean isChanged;
private ObserverWrapper(Observer<T> observer, boolean isFirstSubscribe) {
this.observer = observer;
isChanged = isFirstSubscribe;
}
@Override
public void onChanged(@Nullable T t) {
if (isChanged) {
if (observer != null) {
observer.onChanged(t);
}
} else {
isChanged = true;
}
}
}`
问题再isChanged 和 isFirstSubscribe 标志位上,请修改~
比如在网络请求开始前显示loading 弹窗,网络请求结束时销毁弹窗,ViewModel里面调用Model层获取数据,那么在调用Model层前显示loading,请求结束时销毁loading,而ViewModel不建议应用View层,我该如何通知View层去显示或者销毁loading弹窗呢,暂时我想到的办法是另外每个页面都需要单独定义一个LiveData去设置显示还是销毁,然后View层观察他的值的变化。
想问下有没有更好的办法。
现在demo中包括底层的架构设计都是ViewModel 跟 Repository 一一对应的关系,实际开发过程中比如说qa页面是需要判断有没有登陆的。类似这样的时候是不是就会产生一对多的关系。看了下demo项目中的代码我发现Activity层直接引用了Repository的类。比如一些常量,这样有一些轻微的依赖关系。不用databinding还导致了另外的一个问题就是 在Activity (view 层)要引用到实体类。综上m层跟v层还是存在耦合性。后续需要优化。
如题 只能自己手写了吗 ? 没有别的办法?
希望有什么问题可以直接问你
Cannot create an instance of class com.keisdom.assistant.core.vm.LoginViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2740)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2801)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6372)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
测试手机OPPOR15,首页点击网格布局区域的问答条目进入详情页,快速滑动3页左右,APP闪屏开始进入重启状态,必现
你确认过现在的项目是可以正常编译通过的吗,项目类的结构有错误了,在mvvm这个liably里的base包里和根目录下有一样的类我看了下代码都一样的
点击【活动】 闪退 必现 OPPO R11s android7.1.1 ColorOS版本V3.2
错误日志:
2018-10-10 14:52:11.378 24098-24098/com.code.mvvm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.code.mvvm, PID: 24098
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.code.mvvm/com.code.mvvm.core.view.common.CommonActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2929)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1648)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:185)
at android.app.ActivityThread.main(ActivityThread.java:6615)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:806)
Caused by: java.lang.NullPointerException
at com.code.mvvm.util.Preconditions.checkNotNull(Preconditions.java:12)
at com.code.mvvm.core.vm.ActivityViewModel.getActivityList(ActivityViewModel.java:36)
at com.code.mvvm.core.view.activity.ActivityListFragment.getNetWorkData(ActivityListFragment.java:60)
at com.code.mvvm.core.view.activity.ActivityListFragment.lazyLoad(ActivityListFragment.java:49)
at com.mvvm.base.BaseFragment.onViewCreated(BaseFragment.java:47)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1430)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
at android.app.Activity.performStart(Activity.java:7024)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2929)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1648)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:185)
at android.app.ActivityThread.main(ActivityThread.java:6615)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:806)
如果数据源的LiveData不在ViewModel里, 那么configChange以后, Activity重建后, 也无法从ViewModel里获取到数据, 还要重新请求. 直接通过ViewModel来调用Repository通过LiveDataBus发送数据感觉和MVP的**是一样的. ViewModel里并没有保存数据啊
应该是EVENT_KEY_WORK_MORE, 不是EVENT_KEY_WORK_LIST。
博主的代码风格很赞,除了没注释
2019-11-20 14:39:45.358 3733-3733/? E/DelegateAdapter: 0---0 2019-11-20 14:39:45.362 3733-3733/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.code.mvvm, PID: 3733 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.code.mvvm.widget.banner.BannerItemView.lambda$onBindViewHolder$0(BannerItemView.java:45) at com.code.mvvm.widget.banner.-$$Lambda$BannerItemView$fKWkbLq_F2J7xDSWr9Y1RkJGKeo.bindView(Unknown Source:4) at com.code.mvvm.widget.banner.BannerView.setBannerView(BannerView.java:245) at com.code.mvvm.widget.banner.BannerItemView.onBindViewHolder(BannerItemView.java:43) at com.code.mvvm.widget.banner.BannerItemView.onBindViewHolder(BannerItemView.java:24)
一个页面打开之后需要访问多个接口获取页面数据怎么处理
Issues 上很多问题,在18年后,作者就没回复过,简书博客也停留在18年底 https://www.jianshu.com/u/f181532579b3
知道情况的可以更新一下
1..ViewModel和Repository应该是一对多的关系,这个问题倒是好解决
2..数据与视图绑定这块,有没有什么好的点子,DataBingding虽然有很多缺点,但是数据与视图绑定这块,确实是实现了一种解决方案,虽然写在xml里面有点反人类
com.tqzhang这个包是在哪里导入的???
老哥,关了吧
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.