RecyclerView行的数据滚动时变得混乱

时间:2019-12-24 11:54:04

标签: android android-recyclerview

我有一个显示交易信息的回收站视图。加载后一切正常,但滚动一点后,不同行的内容混合了。 这是我的代码:

    public TransactionAdapter.VHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.cell_transaction_history,parent,false);
        VHolder holder = new VHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull TransactionAdapter.VHolder holder, int position) {
        final Transaction transaction = transactionList.get(position);
        holder.setData(transaction);
    }

    @Override
    public int getItemCount() {
        return transactionList.size();
    }

    private void setTransactionList(ArrayList<Transaction> transactions) {
        this.transactionList = transactions;
    }

还有ViewHolder:

    private class VHolder extends RecyclerView.ViewHolder {
        private AppCompatTextView schemeName;
        private AppCompatTextView amount;
        private AppCompatTextView bank;
        private AppCompatTextView date;
        private AppCompatTextView status;

        VHolder(View itemView) {
            super(itemView);
            schemeName = itemView.findViewById(R.id.scheme_name);
            amount = itemView.findViewById(R.id.amount);
            bank = itemView.findViewById(R.id.bank);
            date = itemView.findViewById(R.id.transaction_date);
            status = itemView.findViewById(R.id.transaction_status);
        }

        private void setData(Transaction transaction) {
            schemeName.setText(transaction.getSchemeName());
            bank.setText(transaction.getBankName());
            date.setText(transaction.getTranDate());

            int amountValue = Integer.parseInt(transaction.getAmount());
            if(amountValue < 0)
                amount.setTextColor(getResources().getColor(R.color.color_status_cancelled));
            else
                amount.setTextColor(getResources().getColor(R.color.color_status_active));
            amount.setText(abs(amountValue));

            Drawable drawable = status.getCompoundDrawablesRelative()[0];
            String st = transaction.getOrderStatus();
            if(st.equalsIgnoreCase(TRAN_STATUS_PROCESSED)){
                drawable.setTint(getResources().getColor(R.color.color_status_active));
                status.setText(TRAN_STATUS_PROCESSED);
            } else {
                drawable.setTint(getResources().getColor(R.color.color_status_cancelled));
                status.setText(TRAN_STATUS_REJECTED);
            }
        }
    }

尝试设置

holder.setIsRecyclable(false);

但我认为这不是理想的解决方案。

更新:

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
setContent(getUserTransactions());

和setContent()方法:

private void setContent(ArrayList<Transaction> transactions) {
    if(adapter == null) {
        adapter = new TransactionAdapter(getLayoutInflater(),transactions);
        recyclerView.setAdapter(adapter);
    } else {
        adapter.setTransactionList(transactions);
    }
    adapter.notifyDataSetChanged();
}

0 个答案:

没有答案