Home > Blockchain >  Kotlin passive - active item rows in recycleview
Kotlin passive - active item rows in recycleview

Time:08-05

I want to create a recyclerview list with active and passive rows, how can I do this in Kotlin? If the data from the table in the Room Database is not the same as the itemtext, it will be inactive and cannot be clicked. For example, while one item is clickable, the other cannot be clicked because this line will be inactive. The data from the other activity will not match the data saved in the database. Is such a situation possible with Kotlin? I wrote the Recycleview list with an adapter. I haven't created the dao part yet.

this my accivity binding and recyclerview code

val recyclerView : RecyclerView = binding.RecyclerView
                recyclerView.layoutManager = GridLayoutManager(this,2)
                val screenSize = resources.configuration.screenLayout and
                        Configuration.SCREENLAYOUT_SIZE_MASK
                if(screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
                    || screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE){
                    recyclerView.layoutManager = GridLayoutManager(this,3)
                }
                recyclerView.adapter = ListAdapter(mutableList1)

This my ListAdapter

override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
        holder.bind(mutableList1[position])
        val Name: String = mutableList1[position]

        holder.itemView.setOnClickListener { v ->
            val intent = Intent(v.context, ControlActivity::class.java)
            ListActivity.mAddress = intent.getStringExtra(BListAdapter.EXTRA_ADDRESS).toString()
            intent.putExtra(Name, Name)
            intent.putExtra(type1, type)
            val string: String = Name
           
            val zerocontrol = string.substring(5,6)
            Log.e ("zerocontrol", zerocontrol)
            val curtainCode: String = if (zerocontrol.toInt() == 0){
                string.substring(6,string.length)
            }else{
                string.substring(5,string.length)
            }
            Log.e("deneme", Code)
            val code = "PR$Code"
            intent.putExtra(Code, code)
            v.context.startActivity(intent)
        }
    }
    override fun getItemCount()= mutableList1.size
    inner class ListViewHolder (private val binding:ListItemBinding):RecyclerView.ViewHolder(binding.root){
        fun bind(item : String){
            binding.apply {
                Name.text = item
               // Log.e("PerdeAdi",item)
            }
        }
    }

CodePudding user response:

Is Depend on which architecture you are using and how you are querying the data Could you please specify more.

CodePudding user response:

I have only single question why do need to capture invalidated data in recycler view? What we can do is just update our data and remove other data. But even if you want to do that then here is a trick.

  1. Add a time field to your model.
data class Student(
    val id :Int,
    val name :String,
    val lastUpdateTime:Long= System.currentTimeInMillies()
){
    fun isOutdated():Boolean{
        return THREE_MINUTES_IN_MILLIES<(System.currentTimeInMillies()-this.lastUpdateTime);
    }
    companion object{
        private const val THREE_MINUTES_IN_MILLIES=3*60*1000;
    }
}
  1. Now don't change the business logic of your http-caching.
  2. Now in your adapter's viewHolder class, add following logic.
inner class ViewHolder2(val binding: NoticeViewBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(student: Student) {
        if(student.isOutdated()){
            //outdated list element
        }else{
           //fresh list element
        }
    }
}

Hopefully you are not updating your ui that fast. This should work. And if it does work then please accept my answer, I am new to answering SO questions. Love from India. ❤️

  • Related