-1

I am using an Adapter to display a list + a static button as a footerView.

The Button as well as the List are Layout Ressource Files loaded via:

LayoutInflater.from(parent.context).inflate(R.layout.recycler_button, parent, false)

I now need to change programatically in some cases the background color of the button as well as the text. How can I manage this?

Here you can find my adapter code:

package com.etask.etaskemployeeapp.Controller.Model

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.etask.etaskemployeeapp.R

class VacationTimeAdapter(val rowJSONList: ArrayList<JSONListRow>) : RecyclerView.Adapter<VacationTimeAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    if (viewType == R.layout.list_layout) {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
        return ViewHolder(v)
    } else {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_button, parent, false)
        return ViewHolder(v)
    }
}

override fun getItemCount(): Int {
    return rowJSONList.size + 1
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    if (position == rowJSONList.size){


    }else {

        val row = rowJSONList.get(position)
        holder.identifierName.text = row.identifier

        // Check which datatype the value for a row is
        if (row.value is String) {
            holder.valueName.text = row.value as String
        } else if (row.value is Double) {
            val value = row.value as Double
            holder.valueName.text = "$value"
        } else if (row.value is Int) {
            val value = row.value as Int
            holder.valueName.text = "$value"
        }
    }
}

override fun getItemViewType(position: Int): Int {
    return if (position === rowJSONList.size) R.layout.recycler_button else R.layout.list_layout
}

// Clean all elements of the recycler
fun clear() {
    rowJSONList.clear()
    notifyDataSetChanged()
}

// Add a list of items -- change to type used
fun addAll(newList: ArrayList<JSONListRow>) {
    rowJSONList.addAll(newList)
    notifyDataSetChanged()
}

class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
    var identifierName = itemView.findViewById<TextView>(R.id.identifierLabel)
    var valueName = itemView.findViewById<TextView>(R.id.valueLabel)

}
}
JVS
  • 2,592
  • 3
  • 19
  • 31
  • you want button on each recyclerview element or just below the list ? – Ashish May 08 '19 at 09:14
  • just below the list. this is already working. but I want to address it's properties – JVS May 08 '19 at 09:59
  • 1
    can you provide some more details like when it will be clickable or do user need to select any element from recyclerview or something like that. so i can provide you some help – Ashish May 08 '19 at 10:05
  • Don't worry about the logic. I just want to access R.layout.recycler_button text and change it – JVS May 08 '19 at 10:13
  • I don't know how to do access the attributes though – JVS May 08 '19 at 10:14
  • 1
    https://stackoverflow.com/a/42897272/10182897 this can help you – Ashish May 08 '19 at 10:17

1 Answers1

0

What are the cases ? You want them to be changed tiggered by an action or when they load? I have such implementation for a bot in Android

  • If it is on load then you can create different items according to a field for example.
  • Otherwise you can create another interface to trigger the change.
hery
  • 49
  • 6
  • I am checking some boolean values and will disable the button if e.g. a boolean's value is FALSE – JVS May 08 '19 at 10:00