实现一个需求很容易,难的是代码的框架,我们应该让它后期的维护变得简单
titlebar
base初始化
private void initBar(TitleBar titleBar) {
StatusBarManager.translucentStatusBar(this, titleBar);
// set the default click listener
titleBar.setLeftClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
titleBar.setLeftImageResource(R.drawable.img_black_back);
titleBar.setTitleColor(Color.WHITE);
//默认给一个白色的背景
titleBar.setBackgroundColor(Color.WHITE);
titleBar.setSubTitleColor(Color.WHITE);
titleBar.setDividerColor(Color.GRAY);
titleBar.setActionTextColor(Color.WHITE);
titleBar.setDividerHeight(0);
}
沉浸式
- 支持版本碎片化
- dark模式和fragment之前的dark模式切换,这是自动的(在父类调度)
- MIUI FLYME等深度定制系统,版本查询和定制话代码需要到官网查询
dark模式自动切换的原理
- 取色器bitmap-palette
- pix像素点色值
- 复写titlebar#setbackground()
loadingHelper
https://www.jianshu.com/p/6a55a888fe58
Activity/Fragment中使用
private void updateCityInfo(Cities.City city) {
mCity = city;
getTitleBar().setLeftText(city.name);
//发起网络加载中
getLoadingHelper().showLoadingView();
ViewModelProviders.of(this).get(HomeModuleViewModule.class)
.loadHomeModule();
}
getLoadingHelper().addRetryListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//网络错误添加action
getLoadingHelper().showLoadingView();
ViewModelProviders.of(HomeTabFragment.this).get(HomeModuleViewModule.class).loadHomeModule();
}
});
ViewModelProviders.of(this).get(HomeModuleViewModule.class).getHomeModule()
.observe(this, new Observer<HomeModule>() {
@Override
public void onChanged(@Nullable HomeModule homeModule) {
if (homeModule == null) {
//网络回调后错误
getLoadingHelper().showNetworkError();
} else {
//网络回调后成功
getLoadingHelper().showContentView();
updateHomeViews(layout_ll_content, homeModule);
if (PUSH_TYPE_REPORT_FROM_HOME) {
PUSH_TYPE_REPORT_FROM_HOME = false;
CreditServiceHelper
.doHomeNextJumpFor(getActivity(), mHeaderView);
}
}
}
});
ViewModel和View-Layout应用
https://www.jianshu.com/p/9dda86b99bcf
ViewModelProviders.of(this).get(CreditCardViewModel.class).getLiveData()
.observe(this, new Observer<CreditlistV102>() {
@Override
public void onChanged(@Nullable CreditlistV102 creditlistV102) {
if (creditlistV102 != null) {
getLoadingHelper().showContentView();
buildViews(creditlistV102);
} else {
getLoadingHelper().showNetworkError();
}
}
});
ViewModelProviders.of(this).get(CreditCardViewModel.class).load();
getLoadingHelper().showLoadingView();
public void buildViews(CreditlistV102 creditlistV102) {
buildBanksView(creditlistV102.bankList);
buildMidTitleView(creditlistV102.cardTitle);
buildViewPagers(creditlistV102.tag_list);
}
private void buildBanksView(List<CreditlistV102.BankListBean> bankList) {
llHeader.addView(new GridHeadView(getContext(), bankList));
}
//分割的view
@BindLayout(R.layout.credit_card_headerview)
static class GridHeadView extends BaseItemViewWithBean<List<CreditlistV102.BankListBean>> {
@BindView(R.id.grid_view)
NoScrollGridView gridView;
@BindView(R.id.icon_bank_bottom)
View iconBankBottom;
@BindView(R.id.ll_spilt)
LinearLayout llSpilt;
boolean mIsExpend;
public GridHeadView(Context context, List<CreditlistV102.BankListBean> data) {
super(context, data);
}
@Override
protected void onViewCreated(View view, final List<CreditlistV102.BankListBean> bean) {
final GridHeadAdapter adapter = new GridHeadAdapter(getContext());
gridView.setAdapter(adapter);
}
}
//adapter
static class GridHeadAdapter extends BaseListAdapter<CreditlistV102.BankListBean> {
public GridHeadAdapter(Context context) {
super(context);
}
@Override
protected void onBindViewHolder(List<Class<? extends BaseViewHolder<CreditlistV102.BankListBean>>>
viewHolderList) {
viewHolderList.add(ViewHolder.class);
}
//ViewHolder
@BindLayout(R.layout.viewholder_card_view_header_banks)
static class ViewHolder extends BaseViewHolder<CreditlistV102.BankListBean> {
@BindView(R.id.img_top)
ImageView imgTop;
@BindView(R.id.tv_name)
TextView tvName;
@Override
protected void bindView(final CreditlistV102.BankListBean bean, int position, final Context context) {
ImageLoader.host(context).load(bean.image, imgTop);
tvName.setText(bean.name);
imgTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CreditBankDetailActivity.invoke(context, bean.id);
}
});
}
}
}
private void buildViewPagers(List<String> cardList) {
viewPager.setAdapter(new CreditCardViewPagerAdapter(getChildFragmentManager(), cardList));
viewPager.setOffscreenPageLimit(cardList.size() - 1);
tabLayout.setupWithViewPager(viewPager);
UIUtil.reflex(tabLayout, 20);
}
RxBus
tag
tags
Model
等可以自定义多个纬度
Action 事件源tag
public interface RxBusAction {
String MAIN_HOME_REFRESH = "main_home_refresh";
String ON_APP_LOGOUT = "on_app_logout";
String ON_APP_LOGIN = "on_app_login";
String ON_CITY_CHANGED = "on_city_changed";
}
消息体
public class RxBusMsg<T> implements Serializable {
public String tag;
public T value;
/**
* @param value 当value为空时必须传入泛型的类型
*/
public RxBusMsg(@NonNull T value) {
this.value = value;
}
public RxBusMsg<String> obtainMsg(String msg) {
return new RxBusMsg<>(msg);
}
}
响应体注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Subscribe {
Tag[] tags() default {};
String tag() default "";
EventThread thread() default EventThread.MAIN_THREAD;
}
https://www.jianshu.com/p/18e420291d4e
网络库
public interface ApiBus {
/**
* 首页
*
* @return
*/
@Url(BASIC_URL + "index/indexv100")
Single<HomeModule> loadHome();
/**
* App启动接口
*
* @param newUserOpen
*
* @return
*/
@Url(BASIC_URL + "config/StartV100")
Single<String> appStart(@Field("new_user_open") String newUserOpen);
/**
* 请求同步数据
*
* @return
*/
@Security(PRIVATE_CRYPT)
@Url(BASIC_URL + "account/SyncV100")
Single<SyncData> LOGIN_SYNC();
}
获得rx被观察者的对象
JtRetrofit.create(ApiBus.class).loadHome();
见VM模式