美文网首页
代码review-曾经踩过的坑,好的编程习惯

代码review-曾经踩过的坑,好的编程习惯

作者: 码农朱同学 | 来源:发表于2018-10-09 14:41 被阅读0次

    1,字符串的replace,substring 等操作一定要先判空处理
    2,需要控件资源的回调必须在生命周期内完成,否则就要及时取消注册 ,防止空指针引起的崩溃

    比如

        protected CompositeDisposable mDisposables = new CompositeDisposable();
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mDisposables != null) {
                mDisposables.clear();
            }
            listenerList.clear();
            listenerList = null;
            mHandler.removeCallbacksAndMessages(null);
            removeFromRefreshCache();
        }
    
            mDisposables.add(RxHttp.call(RequestCommand.COMMAND_HS_RANKING_NEW, params, StockRiseFail::class.java) { stockRiseFail ->
                    for ((index, itemData) in stockRiseFail.list.withIndex()) {
                        stockDataInfoList[index + firstIndex] = itemData
                    }
                    mainAdapter.notifyItemRangeChanged(firstIndex, endIndex)
            })
    

    3,类型转换的问题:

    比如上层是LinearLayout时,需要这个new LinearLayout.LayoutParams

        private void setStatusBarLayout() {
            if (UINotchHelper.has3rdNotch(baseActivity)) {
                rl_login_layout.setLayoutParams(new LinearLayout.LayoutParams(rl_login_layout.getLayoutParams().width, rl_login_layout.getLayoutParams().height + UIStatusBarHelper.getStatusbarHeight(getContext())));
                rl_login_layout.setPadding(rl_login_layout.getPaddingLeft(), rl_login_layout.getPaddingTop() + UIStatusBarHelper.getStatusbarHeight(getContext()), rl_login_layout.getPaddingRight(), rl_login_layout.getPaddingBottom());
            }
        }
    
        private void setStatusBarLayout() {
            if (UINotchHelper.has3rdNotch(baseActivity)) {
                ViewGroup.LayoutParams layoutParams = rl_login_layout.getLayoutParams();
                layoutParams.height += UIStatusBarHelper.getStatusbarHeight(getContext());
                rl_login_layout.setLayoutParams(layoutParams);
                rl_login_layout.setPadding(0, CommonDataManager.getDensityValue(23, baseActivity) + UIStatusBarHelper.getStatusbarHeight(getContext()), 0, CommonDataManager.getDensityValue(20, baseActivity));
            }
        }
    

    相关文章

      网友评论

          本文标题:代码review-曾经踩过的坑,好的编程习惯

          本文链接:https://www.haomeiwen.com/subject/wqneaftx.html