by lazy
private val topCaseAdapter by lazy {
ProfileTopCaseAdapter()
}
private val activity by lazy {
getActivity() as MidderProductContrastActivity
}
回调 函数
定义var pointClick:((remarkDocCaseModel: RemarkDocItemCase)->Unit)? = null;
注册 caseView.pointClick = {
it.xxx)
}
触发 pointClick?.let {
pointClick?.invoke(remarkDocCaseModel)
}
多参数:
var imgClickBlock: ((String, ImageView) -> Unit)? = null //正常图片点
注册
adapter.imgClickBlock = { url, v ->
ViewUtils.showImage(v, arrayListOf(url), context, 0)
}
参数回调
fun processPreLogin(
viewModelScope: CoroutineScope,
userRepo: UserRepository,
results: ((result: Resource<OnePassPreLoginResult>) -> Unit),
): Boolean {
}
数据模型 ?
val data = ArrayList<RemarkDocItemCase?>()
判空
val caseView = holder.itemView as? DocItemCaseView ?: return
for 循环
for (i in 0 until 10) {
}
for (index in 0 until topCaseRv.childCount) {
}
item.bean.list.forEach{
}
for (v in 10 downTo 1 step 3){
Log.d("testFor", ".until.$v")
}
childViewLoop@ for (index in 0 until mAdapter.data.size) {
continue@childViewLoop
if (!result) {
break@innerViewLoop
}
}
when 语句
when ((holder.layoutPosition + 1).toString()) {
"1" -> R.drawable.doc_hot_top1
"2" -> R.drawable.doc_hot_top2
"3" -> R.drawable.doc_hot_top3
else -> 0
}
常量
companion object {
const val FROM_TYPE_TAB = "0"
const val FROM_TYPE_DIALOG = "1"
}
隐藏软键盘
private fun hideKeyboard(rootView: View?) {
var v = rootView
if (v == null) {
v = View(mContext)
}
val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN)
}
run
remarkDocCaseModel.after_operation?.let { ok }?:run{ no }
bean.sh_discount?.let {
if (it.title.isNotBlank()) {
tv_discounts_explain.text = it.title
tv_discounts_explain.setTextColor(DrawableUtils.praceColor(it.title_color, ResUtils.getColor(R.color.color_FF4040)))
} else {
tv_discounts_explain.visibility = View.GONE
}
}?:run {
tv_discounts_explain.visibility = View.GONE
}
RecyclerView
不滑动 android:overScrollMode="never"
as 强转 is 是某种类型
val mView = holder.itemView as? ItemView ?: return
判空 有疑问
//isNotEmpty(str) 等价于 str != null && str.length > 0
//isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length > 0
//即:
//isNotEmpty(str) + && str.trim().length > 0 = isNotBlank(str)
//同理
//isEmpty 等价于 str == null || str.length == 0
//isBlank 等价于 str == null || str.length == 0 || str.trim().length == 0
//
//str.length > 0 && str.trim().length > 0 ---> str.length > 0
协程
continue break
private fun checkExcuteNext(): Boolean {
var result = true
childViewLoop@ for (item in mAdapter.data) {
if (item.itemType == 0)continue@childViewLoop
val viewHolder =
recycleView.findViewHolderForLayoutPosition(item.itemType)
result = (viewHolder?.itemView as ICheckItem).check()
if (!result) {
break@childViewLoop
}
}
return result
}
forEach break
fun main(args: Array<String>) {
val arr = intArrayOf(1,2,3,4,5,6,7)
run breaking@{
arr.forEach continuing@{
if (it == 4) return@breaking
println("value:$it")
}
}
println("this is End")
}
forEach continuing
fun main(args: Array<String>) {
val arr = intArrayOf(1,2,3,4,5,6,7)
arr.forEach continuing@{
if (it == 4) return@continuing
println("value:$it")
}
println("this is End")
}
初始化属性时--Kotlin 的属性委托语法
private val viewModel: LoginViewModel by viewModels()
使用 Elvis 运算符提前从函数返回结果
fun validateAccount(account: Account?) {
val accountName = account?.name?.trim() ?: "Default name"
// account cannot be null beyond this point
account ?: return
}
小细节
https://www.jianshu.com/p/3acd0c96b79c
https://www.sohu.com/a/538859213_121124365