简体   繁体   English

在回收站视图中滚动时数据消失

[英]Data disappears when scrolling in recycler view

Good day.再会。 So I currently have data in my recycler view.所以我目前在我的回收站视图中有数据。 It is for now only static data.目前只有static条数据。 I still have to do the code where I import.我仍然需要在我导入的地方做代码。 My problem however is I have a button that changes the background of a text view.然而,我的问题是我有一个按钮可以更改文本视图的背景。 This happens in my adapter.这发生在我的适配器中。 And when I scroll through my list the bg color change gets reverted back to what it was before the button click.当我滚动浏览我的列表时,bg 颜色变化会恢复到单击按钮之前的状态。 I have read a lot of similar problems but could not really find one that explains clearly or work for me.我已经阅读了很多类似的问题,但无法真正找到一个解释清楚或对我有用的问题。 From what I read the data gets reset to the static data because it is currently happening in my onBindViewHolder and I think this changes the data on every new data read(scrolling).从我读到的数据被重置为 static 数据,因为它目前正在我的 onBindViewHolder 中发生,我认为这会改变每次新数据读取(滚动)时的数据。 I read that I should create a link or a listener and then call it.我读到我应该创建一个链接或一个监听器,然后调用它。 But It does not make sense to me because if a link is called the same amount of times as the code is executed then it will be the same will it not.但这对我来说没有意义,因为如果一个链接被调用的次数与代码的执行次数相同,那么它就会是一样的,不是吗。 Maybe having a condition listener but not sure if this is the way to go.也许有条件侦听器但不确定这是否是通往 go 的方式。

I am somewhat new to android and kotlin. Have been working with it for a month now.我对 android 和 kotlin 有点陌生。已经使用它一个月了。 I dont know everything I am doing but I got given a deadline.我不知道我在做什么,但我得到了最后期限。 So sadly there was no time to go and learn the basics.很遗憾,没有时间去 go 学习基础知识。 Thank you for any and all help.感谢您提供的所有帮助。 Please let me know if you need any additional code/information如果您需要任何其他代码/信息,请告诉我

my adapter我的适配器

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.table_list_item, parent, false)
        return RowViewHolder(itemView)


    }

    private fun setHeaderBg(view: View) {
        view.setBackgroundResource(R.drawable.table_header_cell_bg)
    }

    private fun setContentBg(view: View) {
        view.setBackgroundResource(R.drawable.table_content_cell_bg)
    }

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

      //  (TableViewAdapter.DataviewHolder) .bind()

        val rowPos = holder.adapterPosition



        if (rowPos == 0) {
            // Header Cells. Main Headings appear here
            holder.itemView.apply {
                setHeaderBg(txtWOrder)
                setHeaderBg(txtDElNote)
                setHeaderBg(txtCompany)
              //  setHeaderBg(txtAddress)
                setHeaderBg(txtWeight)
                setHeaderBg(txtbutton1)
                setHeaderBg(txtbutton2)
                setHeaderBg(txttvdone)


                txtWOrder.text = "WOrder"
                txtDElNote.text = "DElNote"
                txtCompany.text = "Company"
               // txtAddress.text = "Address"
                txtWeight.text = "Weight"
                txtbutton1.text = "Delivered"
                txtbutton2.text = "Exception"
                txttvdone.text = ""
            }
        } else {
            val modal = Tripsheetlist[rowPos - 1]

            holder.itemView.apply {
                setContentBg(txtWOrder)
                setContentBg(txtDElNote)
                setContentBg(txtCompany)
              //  setContentBg(txtAddress)
                setContentBg(txtWeight)
                setContentBg(txtbutton1)
                setContentBg(txtbutton2)
                setContentBg(txttvdone)

                txtWOrder.text = modal.WOrder.toString()
                txtDElNote.text = modal.DElNote.toString()
                txtCompany.text = modal.Company.toString()
              //  txtAddress.text = modal.Address.toString()
                txtWeight.text = modal.Weight.toString()
                txtbutton1.text = modal.Button1.toString()
                txtbutton2.text = modal.Button2.toString()
                txttvdone.text = modal.tvdone.toString()
            }
        }

        holder.apply {
            txtbutton1.setOnClickListener {
                Log.e("Clicked", "Successful delivery")
                txttvdone.setBackgroundResource(R.color.green)
                txttvdone.setText("✓")
            }
            txtbutton2.setOnClickListener {
                Log.e("Clicked", "Exception on delivery")
                txttvdone.setBackgroundResource(R.color.orange)
                txttvdone.setText("x")
            }



        }



    }

    class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
         
     val txttvdone:TextView = itemView.findViewById<TextView>(R.id.txttvdone)
         val txtbutton1:Button = itemView.findViewById<Button>(R.id.txtbutton1)
         val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
     }      class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view){


        var txtbutton1 = view.findViewById<Button>(R.id.txtbutton1)
        val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
        var txttvdone = view.findViewById<TextView>(R.id.txttvdone)
    }

I tried (TableViewAdapter.DataviewHolder).bind() doing this and creating another class as I saw that was done in another thread( Why do values disappear after scrolling in Recycler View? ) Its a lot like my problem.我尝试 (TableViewAdapter.DataviewHolder).bind() 这样做并创建另一个 class,因为我看到这是在另一个线程中完成的( 为什么在 Recycler View 中滚动后值会消失? )这很像我的问题。 I just can't seem to implement his solution to make mine work.我似乎无法实施他的解决方案来使我的工作正常进行。 ( don't understand his solution fully) (不完全理解他的解决方案)

//I am also aware that I am using android extensions which will expire at the end of the year. //我也知道我正在使用 android 扩展,该扩展将在今年年底到期。 But for now it works and once I have the code up and running I will start to move over to the newer versions of kotlin.但现在它可以工作,一旦我启动并运行了代码,我将开始转向更新版本的 kotlin。

A RecyclerView, as its name implies, will recycle the views when they go off screen.顾名思义,RecyclerView 将在 go 离开屏幕时回收视图。 This means that when the view for an item comes into view, it gets recreated and the onBindViewHolder() is called to fill in the details.这意味着当一个项目的视图进入视图时,它会被重新创建并调用onBindViewHolder()来填充详细信息。

Your onClickListener inside your adapter changes the background of one of the subviews for your cell view.适配器内的onClickListener会更改单元格视图的其中一个子视图的背景。 However, that cell will be redrawn if it leaves the screen and comes back.但是,如果该单元格离开屏幕并返回,它将被重新绘制。

To get around this, your onClickListener should be changing a property on the data item, and your onBindViewHolder should check that property to determine what background color to display for the subview:为了解决这个问题,您的 onClickListener 应该更改数据项的属性,并且您的onBindViewHolder应该检查该属性以确定子视图显示的背景颜色:

enum class DataState {
    Unselected,
    Success,
    Failure
}
data class DataItem(var state: DataState = DataState.Unselected)

class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
    var dataItems: List<DataItem> = emptyList()

    fun updateData(data: List<DataItem>) {
        dataItems = data
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val dataItem = dataItems[position]
        holder.txttvdone.apply {
            setBackgroundResource(when (dataItem.state) {
                DataState.Unselected -> android.R.color.transparent
                DataState.Success -> R.color.green
                DataState.Failure -> R.color.orange
            })
            text = when (dataItem.state) {
                DataState.Unselected -> ""
                DataState.Success -> "✓"
                DataState.Failure -> "x"
            }
        }

        holder.apply {
            txtbutton1.setOnClickListener {
                Log.e("Clicked", "Successful delivery")
                dataItem.state = DataState.Success
                notifyDataSetChanged()
            }
            txtbutton2.setOnClickListener {
                Log.e("Clicked", "Exception on delivery")
                dataItem.state = DataState.Failure
                notifyDataSetChanged()
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM