<h4>7、${} 正確使用方法</h4>
一個DataBean實體類锦聊,想要打印其中一個屬性值
- println("sth $databean.name") 這是錯誤的寫法 帶"."的都屬于表達式 必須用"{}"包起來不然它會打印出 "sth @databean對象值 .name"
- println("sth ${databean.name}") 這才是正確寫法
<h4>6翰苫、RxJava+Retrofit中Service定義時可變Url @Path("")語法</h4>
<p><code>
/**
* @param type 資源類型
* @param pageNum 每頁數(shù)量
* @param type 頁數(shù)
*/
@GET("data/{type}/{count}/{pageNum}")
fun getGirls(@Path("type") s: String,
@Path("count") count: Int,
@Path("pageNum") pageNum: Int ): Observable<Result<List<GirlBean>>></code></p>
<h4>5征唬、BaseRecyclerViewAdapter封裝</h4>
<p><code>abstract class BaseRecyclerViewAdapter<D, VH : BaseRecyclerViewAdapter.BaseViewHolder>(layoutResId: Int, data: MutableList<D>) : RecyclerView.Adapter<VH>() {
open var layoutResId: Int = 0 //item資源Id
open var data: List<D> = data //數(shù)據(jù)集合
open var view: View? = null
init {
if (layoutResId != 0)
this.layoutResId = layoutResId
else
throw NullPointerException("請設(shè)置Item資源Id")
}
override fun onBindViewHolder(holder: VH, position: Int) {
bindData(holder, data[position],position)
}
override fun getItemCount(): Int {
return data.size
}
/**
* 綁定數(shù)據(jù)
* @param holder
* *
* @param data
*/
protected abstract fun bindData(holder: VH, data: D,position: Int)
open class BaseViewHolder : RecyclerView.ViewHolder{
open var mView: View? = null
constructor(view: View): super(view){
mView = view
}
}
}
</code></p>
<p>在Activity中定義MyRecyclerViewAdapter</p>
<p><code>
inner class MyRecyclerViewAdapter<VH: MyViewHolder>(layoutResId: Int, data: MutableList<GirlBean>) : BaseRecyclerViewAdapter<GirlBean, VH>(layoutResId, data) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
view = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false)
return MyViewHolder(view!!) as VH
}
override fun bindData(holder: VH, data: GirlBean,position: Int) {
this@MainActivity.loadImg(data.url,holder.imageview!!)
holder.textview?.text = data.who
loadImg(data.url, holder.imageview!!)
holder.imageview?.setOnClickListener { mListener.onItemClick(position, holder.imageview!!) }
}
}
inner class MyViewHolder(view: View) : BaseRecyclerViewAdapter.BaseViewHolder(view) {
var imageview: ImageView? = null
var textview: TextView? = null
init {
imageview = mView?.findViewById(R.id.imageview) as ImageView?
textview = mView?.findViewById(R.id.textview) as TextView?
}
}</code></p>
<p>初始化時使用</p>
<p><code>
mAdapter = MyRecyclerViewAdapter(R.layout.item_girl,mList)
recyclerview.adapter = mAdapter
</code></p>
<h4>4、new一個對象(舉例Runnable)</h4>
val mRunnable = object : Runable { }
最后大括號使用lambda
<h4>3诗眨、單例實現(xiàn)</h4>
<code> companion object{
fun getInstance(): SPUtil {
return Inner.instance
}
}
private object Inner{
val instance = SPUtil()
}</code>
調(diào)用:SPUtil.getInstance
<h4>2、Map使用</h4>
kotlin中Map是val不可變類型孕讳,所以沒有put方法
解決方法使用MutableMap可變類型
<h4>1匠楚、跳轉(zhuǎn)Activity</h4>
startActivity(Intent(this, MainActivity::class.java))
注意:使用::反射class類