私は、Bluetoothのデバイスをrecyclerviewで表示するはずのアプリケーションを作成しています。ユーザーがアイテムをクリックしてアクションを実行できるようにしたいと考えています。今のところ私はちょうどクリックでトーストを表示しようとしていますが、後で私はおそらく、ペアを選択するダイアログを表示したいと思います。しかし、私はonclicklistenerの私の使い方にapperentlyなことがありません。私はViewHolderクラスを持っています:DeviceHolderView.OnClickListener
を実装し、Toast.makeText()
の呼び出しをonClickオーバーライド内に配置しています。しかし、何も起こっていません。私はちょうどマイナーなものを逃していると確信して、問題を見つけるのを助けることに感謝します。また、私はkotlinでこれをやっています。これは新しいものです。これを行うためのより効率的なコットリーンタイプの方法があれば、それも助けになるでしょう。私は下に私のコードを掲載しています。前もって感謝します。私のリサイクラビューでカスタムViewHolderのOnClickListenerを実装する方法は?
class DeviceAdapter(val mContext : Context) : RecyclerView.Adapter<DeviceAdapter.DeviceHolder>(){
val mDevices = ArrayList<BluetoothDevice>()
interface OnClickListener{
fun onClick(v: View)
}
fun updateItems(list: ArrayList<BluetoothDevice>){
mDevices.clear()
mDevices.addAll(list)
Log.d(TAG, "updating items : $mDevices")
notifyDataSetChanged()
}
fun ViewGroup.inflate(@LayoutRes res: Int, attachToRoot: Boolean = false): View{
return LayoutInflater.from(mContext).inflate(res, this, attachToRoot)
}
override fun onBindViewHolder(holder: DeviceHolder, position: Int) {
Log.d(TAG, "onBindViewHolder called!")
holder.bindItems(mDevices.get(position))
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DeviceAdapter.DeviceHolder{
Log.d(TAG, "onCreateViewHolder called!")
val v = parent!!.inflate(R.layout.device_item, false)
return DeviceHolder(v)
}
override fun getItemCount(): Int {
return mDevices.size
}
inner class DeviceHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(v: View?) {
Toast.makeText(mContext, "test", Toast.LENGTH_LONG).show()
}
val nameView = itemView.findViewById(R.id.nameView) as TextView
val addrView = itemView.findViewById(R.id.addressView) as TextView
fun bindItems(btDevice: BluetoothDevice) {
Log.d(TAG, "holder created!")
nameView.text = btDevice.name
addrView.text = btDevice.address
itemView.setOnClickListener { this }
}
}
companion object {
val TAG = "Device Adapter"
}
}
は、ここでは、ログメッセージです:
10-09 00:35:50.233 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device found!
10-09 00:35:51.795 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device found!
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: device list : [**:B8:9A:39:1D:**, **:DF:BF:2A:F3:**]
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: updating items : [**:B8:9A:39:1D:**, **:DF:BF:2A:F3:**]
10-09 00:35:56.752 7581-7581/com.example.zemcd.toofxchange D/DiscoveryTask: discovery finished
10-09 00:35:56.762 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onCreateViewHolder called!
10-09 00:35:56.774 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onBindViewHolder called!
10-09 00:35:56.774 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: holder created!
10-09 00:35:56.783 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onCreateViewHolder called!
10-09 00:35:56.786 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: onBindViewHolder called!
10-09 00:35:56.786 7581-7581/com.example.zemcd.toofxchange D/Device Adapter: holder created!
ログには何が表示されますか?あなたはこれらの素晴らしいログステートメントをすべて持っているからです。 – Vucko
@Vucko私は自分のログメッセージを含む編集を投稿しました。 –
アイテムビューはクリック可能ですか? xmlを確認してください。これはすべて正当だと思われる。 – Vucko