2018.6.25
1.异步执行任务,主线程返回
/**
* 使用RxJava异步执行任务,并返回相应的值
*
* @param block 要执行的任务
* @param rtn 任务中的返回值
* @return
*/
fun T.exec(block: T.() -> R, rtn: (R) -> Unit): T {
Observable.create { it.onNext(block()) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { rtn(it) }
return this
}
解析:T,R泛型限定;T.exec 在T上扩展名为exec方法,函数体block为T.(),T.() 执行的函数为T上的任一方法, ->R T.()的函数需要返回R类型的值。函数体rtn为回调函数,(R) 此回调有一个返回值R,-> Unit 函数体无需返回值,java写法:void onCall(R r)。
使用:
exec({
"aaaaaa"//给结果返回
}, { r ->
System.out.println("---返回的结果--- $r")
})
再如数据库操作:
DatabaseManager.getInstance(this).exec({ termDao().getTerms() }, {//termDao()为DatabaseManager中的一个方法,getTerms()会返回List
list.addAll(it); handler.sendEmptyMessage(100)//把执行结果放入list中
})
2.并接相同的方法和接口回调
为网络请求添加相同的方法
fun T.execute(block: T.() -> Observable): Observable {
return block().applySchedulers()
}
fun Observable.applySchedulers(): Observable {
return subscribeOn(Schedulers.io()).
unsubscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread())
}
使用:
fun doLogin(phone: String, pwd: String, onSuccess: (data: UserInfo) -> Unit,
onError: (msg: String, throwable: Throwable) -> Unit) {
ApiServer.execute { doLogin(phone, pwd) }?.subscribe({ bean: UserInfo -> onSuccess(bean) },
{ e: Throwable -> onError("", e) })//执行ApiServer中的doLogin方法,执行完后回调到成功或失败接口
}
3.RecyclerView中的ViewHolder
//定义ViewHolder和复用Item机制
fun View.findViewOften(id: Int): T? {
val viewHolder: SparseArray = tag as? SparseArray ?: SparseArray()
tag = viewHolder
var childView: T? = viewHolder.get(id)
if (null == childView) {
childView = findViewById(id)
viewHolder.put(id, childView)
}
return childView
}
class ViewHolder(private val convertView: View) : RecyclerView.ViewHolder(convertView) {
fun findView(id: Int, t: T): T? = convertView.findViewOften(id)
}
使用
抽象BaseAdapter
abstract class BaseAdapter(var context: Context) : RecyclerView.Adapter() {
protected open val view by lazy { View(context) }
protected open val linearLayout by lazy { LinearLayout(context) }
protected open val imageView by lazy { ImageView(context) }
protected open val textView by lazy { TextView(context) }
protected open val checkBox by lazy { CheckBox(context) }
abstract fun getLayoutResource(): Int
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(getLayoutResource(), parent, false))
}
}
子类继承并使用
class XXXAdapter(context: Context) : BaseAdapter(context) {
override fun getLayoutResource(): Int = R.layout.xxx
override fun getItemCount(): Int = 0
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder?.run {
findView(R.id.tv_alerts_item_type, textView)?.text = "xxxx"
findView(R.id.iv_alerts_item_img, imageView)?.setImageBitmap(bitmap)
}
}
}
2018.12
部分简单的封装
1.try catch
fun T.trycatch(block: T.() -> R?): R? {
return try {
block()
} catch (e: Throwable) {
e.printStackTrace()
null
}
}
使用
trycatch { dbManager?.deleteFace("-2") }//1
faceDao().trycatch { deleteAll() }//2
val _ids = trycatch { getRecordId(num) }//3
2.监听器
protected var listener: ((Int, View, T?) -> Unit)? = null
fun setOnItemClickListener(listener: (position: Int, view: View, t: T?) -> Unit) {
this.listener = listener
}
使用
setOnItemClickListener { _, _, t ->
//xxxxxx
}