2017-10-12 10 views

答えて

1

であるあなたが

のようなインターフェイスを渡すことができ、私のKotlinRecyclerViewアダプタクラス

class ListAdapter(var c: Context, var lists: ArrayList<Employee>, 
    itemClickListener: Any?): RecyclerView.Adapter<RecyclerView.ViewHolder>() { 
     override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder { 
     var v = LayoutInflater.from(c).inflate(R.layout.list_layout, parent, false) 
     return Item(v) 
     } 
     override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) { 
     (holder as Item).bindData(lists.get(position)) 
     } 
     override fun getItemCount(): Int { 
     return lists.size; 
     } 
    class Item(itemView: View) : RecyclerView.ViewHolder(itemView) { 
     fun bindData(_list: Employee) 
     { 
     itemView.textViewfirstname.text = _list.firstName 
     itemView.textViewlastname.text = _list.lastName 
     } 
    } 
    } 

です

list.adapter = ListAdapter(this, getLists(), object : ItemClickInterfaces { 
        override fun itemClick() { 
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 
        } 
       }) 

アダプタ側では、次のように使用できます。

class ListAdapter(var c: Context, var lists: ArrayList<Employee>, itemClickListener: ItemClickInterfaces) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { 


lateinit var itemClickListener: ItemClickInterfaces 

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder { 
    var v = LayoutInflater.from(c).inflate(R.layout.list_layout, parent, false) 
    this.itemClickListener = itemClickListener 
    return Item(v) 
} 

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) { 
    (holder as Item).bindData(lists[position]) 

    if(somcondition){ 
     itemClickListener.itemClick() 
    } 
} 

override fun getItemCount(): Int { 
    return lists.size; 
} 

internal class Item(itemView: View) : RecyclerView.ViewHolder(itemView) { 
    fun bindData(_list: Employee) { 
     itemView.textViewfirstname.text = _list.firstName 
     itemView.textViewlastname.text = _list.lastName 
    } 
} 

関連する問題