realm / realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
realm.io
Apache License 2.0
414 stars 135 forks source link

Kotlin RecyclerView Sections with Realm #160

Closed cherimo closed 3 years ago

cherimo commented 3 years ago

As the title indicates, I am currently working on a RecycleView with a Alphabet letter and a seperate RecycleView with dates. I had some ideas but unfortunately not really bright ones.

Realm objects are immutable so I dont know if its performance wise to clone the objects and modify them, as I like the listener function with animation of the Realm adapter. That means that every update the data needs to be cloned and modified before it will be displayed.

Normally I would loop al the contacts and by every new alphabet, I prepend a header object and process the layout within the bind holder.

Alternative is to loop the Realm data at the beginning and make a index array where all the headers needs to be added and use that array in bindview.

I found some other solutions on the internet but the most are outdated or deprecated.

Is there a way to achieve this performance wise?

This is my adapter code at he moment.

class ContactsAdapter(
    var listVar: RealmResults<SchemeContacts>?,
    val autoUpdate: Boolean,
    val cellClickListener: ContactsList
) : RealmRecyclerViewAdapter<SchemeContacts, ContactsAdapter.DataHolder>(listVar, autoUpdate) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.chat_user, parent, false)
        return DataHolder(view)
    }

    override fun onBindViewHolder(holder: DataHolder, position: Int) {
        val currentItem: SchemeContacts? = listVar?.get(position)
        holder.bindView(currentItem)
        holder.itemView.setOnClickListener {
            cellClickListener.onClickListen(currentItem)
        }

    }

    class DataHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        fun bindView(item: SchemeContacts?) {
            itemView.text1_view.text = item?.contact_name
            itemView.text2_view.text = "ID: "+item?.contact_id
            itemView.text3_view.text = item?.contact_email
        }

    }

    interface CellClickListener {
        fun onCellClickListener(data: SchemeContacts)
    }

}

I look forward to your reactions