从 recyclerview 中删除空格

Posted

技术标签:

【中文标题】从 recyclerview 中删除空格【英文标题】:Remove empty spaces from recyclerview 【发布时间】:2021-11-13 10:37:48 【问题描述】:

我在recyclerview的recycleradapter的viewholder中添加了一些条件来隐藏一些项目 但它显示了recyclerview中已删除项目的空白空间,我隐藏了如何解决这些问题以删除空白空间。如果然后分享我们,还有其他方法可以隐藏recyclerview中的项目。

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)


【问题讨论】:

这能回答你的问题吗? How to insert/remove items from RecyclerView using MVP 我不知道该怎么做? 这能回答你的问题吗? How to hide an item from Recycler View on a particular condition? 【参考方案1】:

您使用的是 android Studio 吗?它应该已经发现了这样的错误。

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

如果您想完全隐藏该项目

bookview.visibility = View.GONE
bookview.layoutParams = ViewGroup.LayoutParams(0,0) //sets width and height of the view

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。 使用后 viewToHide.setLayoutParams(ViewGroup.LayoutParams(0, 0));应用崩溃

以上是关于从 recyclerview 中删除空格的主要内容,如果未能解决你的问题,请参考以下文章

使用单击 Spinner 项刷新 ArrayList 以将新数据显示到 RecyclerView

RecyclerView 挤压子项宽度

如何在 RecyclerView 中实现 StartActivityForResult

RecyclerView 在 BottomSheetFragment 中隐藏其下方的项目

片段内的 RecyclerView 的 Kotlin OnItemClickListener

为啥 recyclerview 的 searchview 不起作用?