0

I have add an some condition in viewholder of recycleradapter of recyclerview to hide some items but it shows empty spaces of removed items in recyclerview which I hide how can I solve these problem to remove empty spaces.is there any another way to hide the items in recyclerview if is then share us.

Bookadapter.kt

class bookadapter(
private var booklist: ArrayList<Booksmodel>,
private val itemClickListener: OnBookItemClicklistner
) : RecyclerView.Adapter<bookadapter.bookholder>() {

var searchText: String = ""


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

fun filterlist(filterlist: ArrayList<Booksmodel>, searchText: String) {

    this.searchText = searchText
    booklist = filterlist

    notifyDataSetChanged()
}

override fun onBindViewHolder(holder: bookholder, position: Int) {
    val view = booklist[position]
    holder.dind(view, itemClickListener)


}

override fun getItemCount(): Int {
    return booklist.size
}

inner class bookholder(view: View) : RecyclerView.ViewHolder(view) {
    val bookname: TextView = view.findViewById(R.id.recbooknametxt)
    val bookpublication = view.findViewById<TextView>(R.id.recbookpubtxt)
    val bookdept = view.findViewById<TextView>(R.id.recbookdepttxt)
    val bookimage = view.findViewById<ImageView>(R.id.recbookimg)
    val bookview = view.findViewById<CardView>(R.id.bookcardView)       

    fun bind(book: Booksmodel, clicklistner: OnBookItemClicklistner) {

        val database = FirebaseDatabase.getInstance()
        val auth = FirebaseAuth.getInstance()
        database.getReference("Users").child(book.UserUID.toString())
            .addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    if (snapshot.value != null) {
                        val usercollege = snapshot.child("College").value.toString()
                        database.getReference("Users")
                            .child(auth.currentUser!!.uid)
                            .addListenerForSingleValueEvent(object :
                                ValueEventListener {
                                override fun onDataChange(snapshot: DataSnapshot) {
                                    if (snapshot.value != null) {
                                        val mycollege =
                                            snapshot.child("College").value.toString()
                                        if (usercollege == mycollege) {
                                            if (searchText.isNotBlank()) {
                                                val highlightedText = book.BookName!!.replace(
                                                    searchText,
                                                    "<font color='red'>$searchText</font>",
                                                    true
                                                )
                                                bookname.text =
                                                    HtmlCompat.fromHtml(
                                                        highlightedText,
                                                        HtmlCompat.FROM_HTML_MODE_LEGACY
                                                    )
                                            } else {
                                                bookname.text = book.BookName
                                            }
                                            //bookname.text=book.BookName
                                            bookpublication.text = book.BookPublication
                                            bookdept.text = book.Department
                                            Picasso.get().load(book.BookImage).into(bookimage)
                                        } else {
                                            bookview.visibility = View.GONE
                                        }
                                    }
                                }

                                override fun onCancelled(error: DatabaseError) {
                                }
                            })
                    }
                }

                override fun onCancelled(error: DatabaseError) {

                }
            })
        itemView.setOnClickListener {
            clicklistner.onBookItemclick(book)
        }
    }
}

interface OnBookItemClicklistner {
    fun onBookItemclick(books: Booksmodel)
}
}
Ashish Dawkhar
  • 131
  • 1
  • 9
  • Does this answer your question? [How to insert/remove items from RecyclerView using MVP](https://stackoverflow.com/questions/39793480/how-to-insert-remove-items-from-recyclerview-using-mvp) – Sweta Jain Sep 19 '21 at 10:51
  • I didn't get how to do it? – Ashish Dawkhar Sep 19 '21 at 12:11
  • Does this answer your question? [How to hide an item from Recycler View on a particular condition?](https://stackoverflow.com/questions/41223413/how-to-hide-an-item-from-recycler-view-on-a-particular-condition) – Ryan M Sep 21 '21 at 09:11

1 Answers1

0

Are you using Android studio?. It should have caught such errors.

override fun onBindViewHolder(holder: bookholder, position: Int) {
    val view = booklist[position]
    //holder.dind(view, itemClickListener) typo
    holder.bind(view, itemClickListener)
}

If you want to hide the item completely

bookview.visibility = View.GONE
bookview.layoutParams = ViewGroup.LayoutParams(0,0) //sets width and height of the view
Rahul Gill
  • 23
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 19 '21 at 14:49
  • after using viewToHide.setLayoutParams(ViewGroup.LayoutParams(0, 0)); app crash – Ashish Dawkhar Sep 22 '21 at 08:57