Я хочу изменить цвет и текст кнопки при нажатии кнопки, без использования ToogleButton. И я установил файл xml следующим образом.
<Button
android:id="@+id/downloadButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Download"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
И я сделал код в основной активности:
override fun onDownloadClick(item: ProjectListDataModel, position: Int) {
downloadButton.setOnClickListener {
if (downloadButton.getText() == "Download") {
downloadButton.setText("Downloaded")
downloadButton.setBackgroundColor(ContextCompat.getColor(this,R.color.blue))
} else if (downloadButton.getText() == "Downloaded") {
downloadButton.setText("Download")
downloadButton.setBackgroundColor(ContextCompat.getColor(this,R.color.gray))
}
}
}
И это Adopter
class ServerProjectsRecyclerAdapter(
val list:List<ProjectListDataModel>,
var serverClickListener: ServerProjectListActivity) : RecyclerView.Adapter<ServerProjectRecyclerViewHolder>() {
interface OnProjectListClickListener {
fun onServerProjectListClick(item: ProjectListDataModel, position: Int)
fun onDownloadClick(item: ProjectListDataModel, position: Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServerProjectRecyclerViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cardview_server, parent, false)
return ServerProjectRecyclerViewHolder(view)
}
override fun getItemCount(): Int {
return list.count()
}
override fun onBindViewHolder(holder: ServerProjectRecyclerViewHolder, position: Int) {
holder.initialize(list.get(position), serverClickListener)
}
}
class ServerProjectRecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun initialize(
item: ProjectListDataModel,
action: ServerProjectListActivity
) {
itemView.projectListView.text = item.name
itemView.modifiedTimeText.text = item.modTime
//itemView.descriptionText.text = item.description
itemView.setOnClickListener {
action.onServerProjectListClick(item, adapterPosition)
}
itemView.downloadButton.setOnClickListener {
action.onDownloadClick(item, adapterPosition)
}
}
}
вроде нет ошибок, но не работает. Есть некоторые примеры использования java, но не Kotlin.
Вот учебник java, что я хочу сделать.
https://www.youtube.com/watch?v=OVmLLet9aAQ
Доза кто-нибудь знает в чем проблема?