本文共 6558 字,大约阅读时间需要 21 分钟。
实现一个需求很容易,难的是代码的框架,我们应该让它后期的维护变得简单
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模式自动切换的原理
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() { @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); } } } });
ViewModelProviders.of(this).get(CreditCardViewModel.class).getLiveData() .observe(this, new Observer() { @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 bankList) { llHeader.addView(new GridHeadView(getContext(), bankList));}//分割的view@BindLayout(R.layout.credit_card_headerview)static class GridHeadView extends BaseItemViewWithBean
> { @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 data) { super(context, data); } @Override protected void onViewCreated(View view, final List bean) { final GridHeadAdapter adapter = new GridHeadAdapter(getContext()); gridView.setAdapter(adapter); }}//adapterstatic class GridHeadAdapter extends BaseListAdapter { public GridHeadAdapter(Context context) { super(context); } @Override protected void onBindViewHolder(List >> viewHolderList) { viewHolderList.add(ViewHolder.class); }//ViewHolder @BindLayout(R.layout.viewholder_card_view_header_banks) static class ViewHolder extends BaseViewHolder { @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 cardList) { viewPager.setAdapter(new CreditCardViewPagerAdapter(getChildFragmentManager(), cardList)); viewPager.setOffscreenPageLimit(cardList.size() - 1); tabLayout.setupWithViewPager(viewPager); UIUtil.reflex(tabLayout, 20); }
tag
tags Model 等可以自定义多个纬度Action 事件源tagpublic 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 RxBusMsgimplements Serializable { public String tag; public T value; /** * @param value 当value为空时必须传入泛型的类型 */ public RxBusMsg(@NonNull T value) { this.value = value; } public RxBusMsg 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;}
public interface ApiBus { /** * 首页 * * @return */ @Url(BASIC_URL + "index/indexv100") SingleloadHome(); /** * App启动接口 * * @param newUserOpen * * @return */ @Url(BASIC_URL + "config/StartV100") Single appStart(@Field("new_user_open") String newUserOpen); /** * 请求同步数据 * * @return */ @Security(PRIVATE_CRYPT) @Url(BASIC_URL + "account/SyncV100") Single LOGIN_SYNC();}获得rx被观察者的对象 JtRetrofit.create(ApiBus.class).loadHome();见VM模式
转载地址:http://aaasx.baihongyu.com/