简体   繁体   English

Recyclerview Android Kotlin滚动问题内的EditText和TextView

[英]Edittext and textview inside recyclerview Android kotlin scrolling issue

This is the kotlin code where I have a order cart which consists of an ArrayList of elements and what I am trying to do here is that by default there will be quantity set to 1 in the EditText and I have buttons to increment and decrement it. 这是科特林代码,在这里我有一个订单购物车,其中包含一个元素的ArrayList ,我在这里尝试做的是默认情况下EditText中的数量将设置为1,并且我有一些按钮来增加和减少它。 So whenever I try to increment or decrement the value using those buttons the total amount for the TextView in each row must be changed. 因此,每当我尝试使用这些按钮递增或递减值时,必须更改每行TextView的总数。 However now everything is working fine like whenever I increment or decrement the value the TextView total amount changes in a row but when I scroll down or up the value in the TextView disappears and is set back to its default value. 但是,现在一切正常,就像每当我增加或减少该值时, TextView总量连续更改,但是当我向下或向上滚动时, TextView的值就会消失,并重新设置为其默认值。

internal class KartListAdapter(private var arrayList: List<Kartdata>,
                           private val context: Context,
                           var presenter: MainContract.Presenter,
                           var recyclerview: RecyclerView,grandtotalTv:TextView) : RecyclerView.Adapter<KartListAdapter.MainHolder>() {

    var list=arrayList

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

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

        holder.bindItem(arrayList[position])
        holder.itemsqty.tag = position
        holder.perpriceprice.tag=position

        var qtyval:Int = holder.itemsqty.text.toString().toInt()
        var perqtyPrice = arrayList[position].qtyPrice

        holder.btnIncrease.setOnClickListener() {
            qtyval++
            holder.itemsqty.text=qtyval.toString()
        }

        holder.btnDecrease.setOnClickListener() {
            qtyval--
            holder.itemsqty.text=qtyval.toString()
        }

        holder.itemsqty.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
               // Toast.makeText(context,"after text change"+s.toString(),Toast.LENGTH_LONG).show()
            }

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
                //Toast.makeText(context,"before text change"+s.toString(),Toast.LENGTH_LONG).show()
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
                if(s.toString().trim()!="") {
                    if (s.toString().toInt()<1) {
                        val perpiecetotal=qtyval*perqtyPrice
                        holder.itemsqty.text="1"
                        holder.perpriceprice.text="OMR:"+perpiecetotal.toString()

                    } else {
                        val perpiecetotal=qtyval*perqtyPrice
                        Log.i("pricing-qtyval",qtyval.toString())
                        Log.i("pricing-perqtyprice",perqtyPrice.toString())
                        holder.perpriceprice.text="OMR:"+perpiecetotal.toString()
                        Log.i("pricing-grandtotalprice",perpiecetotal.toString())
                        holder.perpriceprice.tag=position
                    }
                    //Toast.makeText(context,"on text change"+s.toString(),Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(context,"somewhere qty is empty",Toast.LENGTH_LONG).show()

                }
            }
        })
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

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

    internal inner class MainHolder(view: View) : RecyclerView.ViewHolder(view) {

        var menuname = itemView.findViewById<TextView>(R.id.cateegory_tv)
        var qtyname = itemView.findViewById<TextView>(R.id.qty_name)
        var qtyprice = itemView.findViewById<TextView>(R.id.qty_price)
        var perpriceprice = itemView.findViewById<TextView>(R.id.price_calculated_tv_item)
        var itemsqty = itemView.findViewById<TextView>(R.id.no_of_items_edt)
        var deleteicon = itemView.findViewById<ImageView>(R.id.delete_icon)
        var btnIncrease = itemView.findViewById<ImageView>(R.id.btn_increase)
        var btnDecrease = itemView.findViewById<ImageView>(R.id.btn_decrease)

        fun bindItem(kart: Kartdata) {
            menuname.setText(kart.MenuName)
            qtyname.setText(kart.qtyName)
            qtyprice.setText("OMR\t"+kart.qtyPrice)
            val perpiecetotal=1*kart.qtyPrice
            perpriceprice.text="OMR:"+perpiecetotal.toString()

            deleteicon.setOnClickListener() {
                val database = KartDatabase.getInstance(context)
                val repository = KartRepository(AppExecutors(), database.dapendukDAO())
                Toast.makeText(context,"deleted",Toast.LENGTH_SHORT).show()
                presenter.deleteitem(repository,kart.id)
                presenter.getlistsize(repository)
                (context as FragmentActivity).supportFragmentManager.beginTransaction()
                        .disallowAddToBackStack()
                        .replace(R.id.container, KartFragment.newInstance(), KartFragment.TAG)
                        .commit()
            }
        }
    }
}

You need to have an array of quantities in the KartListAdapter to save the quantities. 您需要在KartListAdapter具有数量数组才能保存数量。 The values are disappearing an showing wrongly is because the views are getting recycled in the RecyclerView . 值消失了,这是错误的显示,因为视图在RecyclerView中被RecyclerView So you need to save the sates of each row in a separate ArrayList and then show the quantities from there. 因此,您需要将每行的状态保存在单独的ArrayList中,然后从那里显示数量。

Hence I would like to propose having a separate ArrayList in your KartListAdapter like var quantityList: List<Int> . 因此,我想建议在您的KartListAdapter有一个单独的ArrayList ,如var quantityList: List<Int> KartListAdapter var quantityList: List<Int>

Then when a quantity is being increased or decreased, have the value in the quantityList . 然后,当数量增加或减少时,将值包含在quantityList Initialize this ArrayList with zeros and the size equals to the arrayList passed to the adapter. 用零初始化此ArrayList,其大小等于传递给适配器的arrayList

Now in your bindView function, set the text in the quantity text field like this. 现在,在bindView函数中,像这样在数量文本字段中设置文本。

holder.itemsqty.text = quantityList[position];

And insider onBindViewHolder , while you are updating the quantities, just increase and decrease the values in the quantityList and then call notifyDataSetChanged to make the changes required in your RecyclerView . 在更新数量时,内部人员onBindViewHolder只需增加和减少quantityList的值,然后调用notifyDataSetChanged即可在RecyclerView进行所需的更改。

holder.btnIncrease.setOnClickListener() {
    quantityList[position] = quantityList[position] + 1;
    notifyDataSetChanged();
}

holder.btnDecrease.setOnClickListener() {
    if(quantityList[position] > 0) { 
        quantityList[position] = quantityList[position] - 1;
        notifyDataSetChanged();
    }
}

The code is not tested and might not work due to syntax errors. 该代码未经测试,由于语法错误,可能无法正常工作。 However, I think you get the idea of storing the quantities increased or decreased in a separate list and populate the views from there in your RecyclerView . 但是,我认为您可以将增加或减少的数量存储在一个单独的列表中,然后在RecyclerView填充视图。

Hope that helps! 希望有帮助!

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

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