代码设计思路这个方面需要持续跟进优化的,
这里不讲大的方面。
主要是总结实际开发中的感悟和积累
我觉得可以根据需求的大小可以针对性的整理一下
一:小的需求
增加一个功能点
二:稍大的需求
比如提供对大众点评的数据在地图上分类展示并交互的功能
这个就需要进行类的设计+时序分析了
三:一般的需求
比如增加个聊天的功能
需求分析 +类的设计+时序分析了
四:较大的需求
重构导航app,能够兼容海外地图的开发
需求分析 +类的设计+时序分析+人员分工
先看第一种情况:增加个状态更新的按钮或数据显示列表
这种需求,我要怎么弄
三个步骤:
1 找到事件触发的地方(事件通知或回调里)
2 找到需要显示的地方(布局里)
3 通过什么方式将事件内容发给显示。
这种你有什么需要注意的,或者记录的:
1 要能放到原来的架构体系里,里面有MutableLiveData 就用MutableLiveData通知view显示
有mvp的抽象接口,就在抽象接口增加一个方法
最后再考虑自己增加intaface接口调用,或直接强依赖(直接引用view类或直接用handle转主线程引用view类)
2 要尽量复用,比如其他页面也有这块的显示,是不是可以将共同的功能放到基类view里
3 要尽量减少和其他的联系,是不是可以将view的处理逻辑封装到自定义view里,甚至不需要和其他类联系(注册eventbus,监听各类事件就行了)
写的比较虚,得写个实例来强化以下
主页右上方展示一个收藏列表,列表要有上下要有指定的过渡效果,该收藏列表设置页面已经实现了的
首先看常规方式:
<RelativeLayout
android:layout_width="@dimen/auto_dimen2_510"
android:layout_height="@dimen/auto_dimen2_250"
>
<RecyclerView
android:id="@+id/favlistss"
style="@style/scrollbar_style"
android:layout_width="@dimen/auto_dimen2_510"
android:layout_height="@dimen/auto_dimen2_250"
android:divider="@null"
android:dividerHeight="0dp"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/auto_dimen2_20"
android:background="@drawable/mask_up_510"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/auto_dimen2_20"
android:background="@drawable/mask_down_510"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
代码加载数据:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new SearchFavoritePoiAdapter());
SearchManager.getInstance().getSimpleFavoriteList(FavoriteType.FavoriteTypePoi, new Callback(){
public void onResult(PoiList poiList){
handler.post()->{
searchFavoritePoiAdapter.updateData(poiList);
}
}
});
这个是 之前的设置页面做的,
1 那么为了快速实现,常规做法:
copy,就是将xml和代码各自复制到主页,问题解决,耗时 5分钟
2 复用方式:
xml复用就不多说了: <include layout="@layout/favlist"/> (往往配合merge使用)
自定义view复用:
自定义复用后的用法只有一句话: EventBus.getDefault().post(new FavEvent());
这种方式好多了,甚至不需要代码里拿id,但耗时应该是需要50分钟以上的。
下面是我的自定义view
/**
* 将recyclerView配置封装进来
* 将数据的加载显示流程封装进来
* 将对外接口独立出来,仅接受event消息
* 将过渡效果开放出去
*/
public class SkinRecyclerView extends RelativeLayout implements IShadowView {
private SearchFavoritePoiAdapter adapter;
private TextView shadowUp,shadowDown;
private RecyclerView recyclerView;
private Handler handler = new android.os.Handler(Looper.getMainLooper());
public SkinRecyclerView(Context context) {
super(context);
EventBus.getDefault().register(this);
init();
}
public SkinRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SkinRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
View.inflate(context, R.layout.layout_license_plate_frame, this);
shadowUp = findViewById(R.id.shadowUp);
shadowDown = findViewById(R.id.shadowDown);
recyclerView = findViewById(R.id.recyclerView);
init();
}
//将 recyclerView 配置封装进来
private void init() {
adapter = new SearchFavoritePoiAdapter();
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new SearchFavoritePoiAdapter());
}
private void updateData(List<PoiList> poiList){
adapter.setList(poiList);
adapter.notifyDataSetChanged();
}
//设置 过渡效果
@Override
public void setShadowVisibility(int visibility) {
shadowUp.setVisibility(visibility);
shadowDown.setVisibility(visibility);
}
//将数据的加载显示流程封装进来,暂不对外开放,以eventbus为唯一接口
private void startLoadData(){
SearchManager.getInstance().getSimpleFavoriteList(FavoriteType.FavoriteTypePoi, new Callback(){
public void onResult(PoiList poiList){
handler.post(()->{
updateData(poiList);
});
}
});
}
//将对外接口独立出来,仅接受event消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void eventReceiveNumber(FavEvent favevent) {
startLoadData();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
EventBus.getDefault().unregister(this);
handler.removeCallbacksAndMessages(null);
}
}
这次就只针对第一种需求进行分析了,接下来第二,第三,第四。