向下滚动时,RecyclerView Items的值会重置

时间:2018-05-14 10:57:54

标签: android android-recyclerview kotlin recycler-adapter

这是我想要做的事情的场景:我正在创建一个RecyclerView,它可以添加或删除一个或多个具有EditText但是......的子项/片段/视图。

这是我的问题:每当我在RecyclerView中向下滚动时,其项目的值都会重置。我认为这里的问题是RecyclerView适配器无法绑定或保存值,但我不知道我的代码中究竟出现了什么问题。

class StocksAdapter() : RecyclerView.Adapter<ViewHolder>() {


class StockFragmentHolder : ViewHolder {
    fun Double.format(digits: Int) = java.lang.String.format("%,.${digits}f", this)


    lateinit var numberOfShares: EditText;
    lateinit var buyPrice: EditText;
    lateinit var btnCompute: Button
    lateinit var btnRemove: Button
    lateinit var stockAveragePrice: TextView
    lateinit var stockTotalAmount: TextView
    private var savedStock : Stock? = null

    constructor(view: View) : super(view) {
        numberOfShares = view.findViewById(R.id.stockNumberOfShares)
        buyPrice = view.findViewById(R.id.stockBuyPrice)
        btnCompute = view.findViewById(R.id.btnCompute)
        btnRemove = view.findViewById(R.id.btnRemove)
        stockAveragePrice = view.findViewById(R.id.stockAveragePrice)
        stockTotalAmount = view.findViewById(R.id.stockTotalAmount)
    }


    fun populateView(stock: Stock, onStocksAdapterListener: OnStocksAdapterListener, position: Int) {

        if(savedStock == null || stock.buyPrice > 0 || stock.numberOfShares > 0) {
            savedStock = stock
        }

        this.buyPrice.setText(if (savedStock!!.buyPrice <= 0.0) "" else savedStock!!.buyPrice.toString())
        this.numberOfShares.setText(if (savedStock!!.numberOfShares <= 0.0) "" else savedStock!!.numberOfShares.toString())
        this.stockAveragePrice.text = "0"
        this.stockTotalAmount.text = "0"

        this.btnCompute.setOnClickListener(View.OnClickListener {

            var stock = Stock();

            stock.numberOfShares = this.numberOfShares.text.toString().toLongOrNull()
            stock.buyPrice = this.buyPrice?.text.toString().toDoubleOrNull()
            stock.sellPrice = 0.0


            if (stock.numberOfShares != null || stock.numberOfShares != 0L) {

                var buyTotalAmount: Double = 0.0;
                buyTotalAmount = StocksCalculator.calculateTotalSharesPrice(stock, StocksCalculator.TRANSACTION_FEE_BASE_VALUES, Constants.TRANSACTION_TYPE.TRANSACTION_TYPE_BUY)

                var averagePricePerShare = (buyTotalAmount / stock.numberOfShares)

                this.stockAveragePrice.text = averagePricePerShare.format(2);
                this.stockTotalAmount.text = "" + buyTotalAmount.format(2)

                onStocksAdapterListener.onStocksComputeButtonClicked(stock.numberOfShares, averagePricePerShare, buyTotalAmount, position)
            }
        })

        this.btnRemove.setOnClickListener(View.OnClickListener {
            onStocksAdapterListener.onStocksRemoveButtonClicked(position)
        })

    }
}


lateinit var onStocksAdapterListener: OnStocksAdapterListener;
lateinit var listStocks: ArrayList<Stock>

public constructor(listStocks: ArrayList<Stock>, onStocksAdapterListener: OnStocksAdapterListener) : this() {
    this.listStocks = listStocks
    this.onStocksAdapterListener = onStocksAdapterListener
    setHasStableIds(true)

}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
    var itemView = LayoutInflater.from(parent?.getContext())
            .inflate(R.layout.fragment_stock, parent, false)
}

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


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

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
    var stock: Stock = listStocks.get(position)
    holder.populateView(stock, this.onStocksAdapterListener, position)
}


public interface OnStocksAdapterListener {
    public fun onStocksAddButtonClicked()

    public fun onStocksComputeButtonClicked(numberOfShares: Long?, averagePricePerShare: Double?, averageTotalAmount: Double?, position: Int)

    public fun onStocksRemoveButtonClicked(position: Int)
}

以下是我在片段中创建recyclerView的方法:

    val mLayoutManager = LinearLayoutManager(this.context)
    var recyclerView: RecyclerView = view.findViewById(R.id.recyclerView)

    val itemDecorator = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    itemDecorator.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider))
    recyclerView.addItemDecoration(itemDecorator)
    recyclerView.setLayoutManager(mLayoutManager)
    recyclerView.setItemAnimator(DefaultItemAnimator())
    recyclerView.adapter = stocksAdapter

我已尝试禁用ViewHolder的setIsRecyclable,但问题仍然存在。

阻止RecyclerView停止重置值的最佳方法是什么?或者我应该用ListView替换RecyclerView?

3 个答案:

答案 0 :(得分:1)

正如我在您的代码中看到的那样,您尝试在活动加载后添加项目。因此,当您滚动活动时,它再次调用onCreate活动,数据再次从存储或网络加载,因此以特定方法或OnActiviyt Result方法加载数据。

答案 1 :(得分:1)

我通过增加recyclerview的缓存大小找到了一种解决方法。只需确保缓存大小足以满足您的应用程序。

代码:

recyclerView.setItemViewCacheSize(int);

答案 2 :(得分:0)

我在代码中不知道savedStock是什么或者你用它做什么。但是您的populateView方法无法按预期工作。

如果您只使用此savedStock进行渲染,请将其完全删除,否则请勿在{{1​​}}中使用它。

populateView取决于根据您获得的位置传递的populateView

您的代码应该是

stock