对github上别人项目的分析。觉得他框架搭的不错。好好向他学习学习。
https://github.com/spring2613/Daily
项目亮点颇多,自己找了自己觉得不错的点,分析分析。
下面就开始分析一下吧:
我分析代码通常从Application开始分析起来,那么就先看看别人的Application怎么做的吧。
清单文件中:
".App"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/BaseAppTheme">
</application>
Android application 和 activity 标签详解:http://blog.csdn.net/self_study/article/details/54020909
Application中,也是App
也就是各种初始化。
清单文件中:
<activity
android:name=".mvp.ui.activities.SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
intent-filter>
activity>
最先启动的Activity配置
关于Intent详解:http://blog.csdn.net/u012637501/article/details/41080891
再来看看这个SplashActivity:
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {
protected T mPresenter;
public abstract int getLayoutId();
public abstract void initInjector();
public abstract void initViews();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
这么多东西结合一起玩。感觉对于现在主流来说是必须会玩的。
跟Activity差不多也是通过泛型跟Presenter关联
protected void setItemAppearAnimation(RecyclerView.ViewHolder holder, int position, @AnimRes int type) {
if (position > mLastPosition/* && !isFooterPosition(position)*/) {
Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), type);
holder.itemView.startAnimation(animation);
mLastPosition = position;
}
}
精彩用法:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view;
switch (viewType) {
case TYPE_FOOTER:
view = getView(parent, R.layout.item_news_footer);
return new FooterViewHolder(view);
case TYPE_ITEM:
view = getView(parent, R.layout.item_news);
final ItemViewHolder itemViewHolder = new ItemViewHolder(view);
setItemOnClickEvent(itemViewHolder, false);
return itemViewHolder;
case TYPE_PHOTO_ITEM:
view = getView(parent, R.layout.item_news_photo);
final PhotoViewHolder photoItemViewHolder = new PhotoViewHolder(view);
setItemOnClickEvent(photoItemViewHolder, true);
return photoItemViewHolder;
default:
throw new RuntimeException("there is no type that matches the type " +
viewType + " + make sure your using types correctly");
}
}
Google推荐的图片加载库Glide
public interface NewsInteractor {
Subscription lodeNewsChannels(RequestCallBack callBack);
}
用到了dagger的@Inject
public class NewsInteractorImpl implements NewsInteractor> {
@Inject
public NewsInteractorImpl() {
}
@Override
public Subscription lodeNewsChannels(final RequestCallBack> callBack) {
return Observable
.create(new Observable.OnSubscribe>() {
@Override
public void call(Subscriber super List> subscriber) {
NewsChannelTableManager.initDB();
subscriber.onNext(NewsChannelTableManager.loadNewsChannelsMine());
subscriber.onCompleted();
}
})
.compose(TransformUtils.>defaultSchedulers())
.subscribe(new Subscriber>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
callBack.onError(App.getAppContext().getString(R.string.db_error));
}
@Override
public void onNext(List newsChannelTables) {
callBack.success(newsChannelTables);
}
}
);
}
}
repository(仓库)中分为(db)和(network)
NewsChannelTableManager属于从db repository中获取数据
public interface NewsListInteractor {
Subscription loadNews(RequestCallBack listener, String type, String id, int startPage);
}
public class NewsListInteractorImpl implements NewsListInteractor> {
// private boolean mIsNetError;
@Inject
public NewsListInteractorImpl() {
}
@Override
public Subscription loadNews(final RequestCallBack> listener, String type,
final String id, int startPage) {
// mIsNetError = false;
// 对API调用了observeOn(MainThread)之后,线程会跑在主线程上,包括onComplete也是,
// unsubscribe也在主线程,然后如果这时候调用call.cancel会导致NetworkOnMainThreadException
// 加一句unsubscribeOn(io)
return RetrofitManager.getInstance(HostType.NETEASE_NEWS_VIDEO).getNewsListObservable(type, id, startPage)
.flatMap(new Func1
Daily这算是别人做的框架模板,里面有很多知识点。
对于我来说挺陌生的,还得下功夫一个一个学好。
MVP + Rx +Retrofit+OkHttp3 +Dagger入门的一个很好的demo
如果+上模块化,组件化,那些玩意就更厉害了。