Kotlin编写RecyclerView的Adapter
kotlin在现在的Android开发中已经较为常见,空闲之余学习一下还是非常必要的,没准儿哪天就可以利用它进行些骚操作呢(梦想还是要有的)。
利用Kotlin编写一个简单的Adapter
class SimpleAdapter(private val context: Context, private val data: List<String>) : RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder>() {
override fun onBindViewHolder(holder: SimpleViewHolder?, position: Int) {
holder?.textView?.text = data[position]
}
override fun getItemCount(): Int = data.size
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SimpleViewHolder
= SimpleViewHolder(LayoutInflater.from(context)
.inflate(R.layout.item_recycler, parent, false))
inner class SimpleViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var textView: TextView = view.findViewById(R.id.item_tv)
}
}
上面adapter完成了一个List集合数据的简单绑定,是不是发现相对于用Java实现同样功能代码更为精简。
在onBindViewHolder方法里面调用如下代码
holder?.textView?.setOnClickListener {
Toast.makeText(context, "click", Toast.LENGTH_SHORT).show()
}
为item中的控件设置监听器,至此一个简单的adapter编写完成。
Adapter中包含不同item类型
废话不多说直接上代码
class RecyclerAdapter(private val context: Context, private val data: List<String>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val HEADER =
private val NORMAL =
private val FOOTER =
override fun getItemCount(): Int = data.size
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? = when (viewType) {
HEADER -> {
HeaderViewHolder(LayoutInflater.from(context).
inflate(R.layout.item_header, parent, false))
}
FOOTER -> {
FooterViewHolder(LayoutInflater.from(context)
.inflate(R.layout.item_footer, parent, false))
}
else -> {
RecyclerViewHolder(LayoutInflater.from(context)
.inflate(R.layout.item_recycler, parent, false))
}
}
/**
* 利用kotlin中的when代替Java中的if/else
* 返回不同的item类型
*/
override fun getItemViewType(position: Int): Int = when (position) {
-> {
HEADER
}
itemCount - -> {
FOOTER
}
else -> {
NORMAL
}
}
/**
* 根据不同的item类型绑定不同的数据
*/
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
when (getItemViewType(position)) {
HEADER -> {
if (holder is HeaderViewHolder) {
holder.tv.text = "I am Header"
holder.tv.setOnClickListener {
Toast.makeText(context, "I am Header", Toast.LENGTH_SHORT).show()
}
}
}
FOOTER -> {
if (holder is FooterViewHolder) {
holder.tv.text = "I am Footer"
}
}
else -> {
if (holder is RecyclerViewHolder) {
holder?.textView?.text = data[position]
holder.textView.setOnClickListener {
Toast.makeText(context, "click", Toast.LENGTH_SHORT).show()
}
}
}
}
}
inner class RecyclerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var textView: TextView = view.findViewById(R.id.item_tv)
}
class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var tv: TextView = view.findViewById(R.id.tv_header)
}
inner class FooterViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var tv: TextView = view.findViewById(R.id.tv_footer)
}
}
全文字随便来张效果图吧
如果某个item中控件的监听器我不想写在或者不能写在(更新view某些原因导致的)onBindViewHolder方法中,ViewHolder该怎么写?
inner class FooterViewHolderTwo : RecyclerView.ViewHolder {
constructor(view: View) : super(view) {
tv = view.findViewById(R.id.tv_footer)
tv.setOnClickListener {
Toast.makeText(context, "I am Footer", Toast.LENGTH_SHORT).show()
}
}
var tv: TextView
}
注意:类FooterViewHolderTwo需要访问类RecyclerAdapter的成员context,需要用 inner 声明,不然是访问不到的哟!
接口的声明与调用
interface MyInterface {
fun test(position: Int)
}
var myInterface: MyInterface? = null
fun setListener(myInterface: MyInterface?) {
this.myInterface = myInterface
}
很常规的操作
至此,关于Kotlin编写RecyclerView的Adapter就已介绍完毕!