2つの要素を持つリサイクラービューがあります。異なることを行うために2つのボタンを追加したい(リサイクラービューのどの要素に依存します。それを行うにはどうすればよいですか?私の要素はテキストビューです)
SettingsActiv.kt
class SettingsActiv : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
val arrow_backBtn = findViewById(R.id.settingsToolbarBtn) as Button
arrow_backBtn.setOnClickListener{
startActivity(Intent(this, MainActivity::class.Java))
}
val linearLayout = findViewById(R.id.constr) as LinearLayout
val recyclerView = RecyclerView(this)
val series = ArrayList<SeriesModel>()
recyclerView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
series.add(SeriesModel("Unit of length","Meter"))
series.add(SeriesModel("Unit of temperature","Celcius"))
val adapter = MyAdapter(series)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
recyclerView.adapter = adapter
linearLayout.addView(recyclerView,1)
}
MyAdapter.kt
class MyAdapter(private val seriesList: ArrayList<SeriesModel>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.activity_settings_child, parent, false)
return MyViewHolder(v)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textViewName.text = seriesList[position].name
holder.textViewNameDesc.text = seriesList[position].desc
}
override fun getItemCount(): Int {
return seriesList.size
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewName = itemView.findViewById(R.id.name) as TextView
val textViewNameDesc = itemView.findViewById(R.id.desc) as TextView
}
}
これを試して
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textViewName.text = seriesList[position].name
holder.textViewNameDesc.text = seriesList[position].desc
holder.textViewName.setOnClickListener {
Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show()
}
}