Android - Recycler View项目在滚动时被交换

时间:2017-01-18 04:52:33

标签: android android-recyclerview recycler-adapter

有人可以解释是什么问题。

我有一个回收网格视图,其中每个项目都包含倒数计时器和视图,这些视图在倒计时完成后会发生变化。

但是当我向下滚动并返回顶部时出现问题,我的项目对列表中的其他项目进行了更改,如果我再次滚动,则会再次更改项目。

这是我的适配器。

public class TableTransactionRecyclerView extends RecyclerView.Adapter<TableTransactionRecyclerView.ViewHolder> {

    private ArrayList<TableInfo> tableList;
    private OnItemClickListener listener;
    private static Context context;
    private static SharedPreferences preferences;

    private static List<ViewHolder> lstHolders;
    public Runnable updateRemainingTimeRunnable = new Runnable() {
        @Override
        public void run() {
            synchronized (lstHolders) {
                for (ViewHolder holder : lstHolders) {
                    holder.updateTimeRemaining();
                }
            }
        }
    };

    public TableTransactionRecyclerView(Context context, ArrayList<TableInfo> tableList, OnItemClickListener listener) {
        this.tableList = tableList;
        this.listener = listener;
        this.context = context;
        preferences = context.getSharedPreferences(CommonConsts.MY_PREFERENCES, Context.MODE_PRIVATE);

        lstHolders = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder vh;

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.table_transaction_row_layout, parent, false);
        vh = new ViewHolder(itemView);
        synchronized (lstHolders) {
            lstHolders.add(vh);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(tableList, position, listener);
    }

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

    public static class ViewHolder extends  RecyclerView.ViewHolder {
        private TextView textView;
        public ImageButton popupMenuBtn;
        private ProgressBar showLoadingImage;
        private ImageView successTick;
        private ImageView tableIcon;
        private LinearLayout processingLayout;
        private View lineView;
        private LatoTextView transactionAmountTextView;
        private LatoTextView timerCountTextView;
        private ArrayList<TableInfo> tableList;
        private int position;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.table_id_text_view);
            popupMenuBtn = (ImageButton) itemView.findViewById(R.id.pop_menu_btn);
            showLoadingImage = (ProgressBar) itemView.findViewById(R.id.show_loading_image);
            successTick = (ImageView) itemView.findViewById(R.id.success_image_view);
            lineView = itemView.findViewById(R.id.view_id);
            tableIcon = (ImageView) itemView.findViewById(R.id.table_icon);
            processingLayout = (LinearLayout) itemView.findViewById(R.id.processing_layout);
            transactionAmountTextView = (LatoTextView) itemView.findViewById(R.id.transaction_amount_text_view);
            timerCountTextView = (LatoTextView) itemView.findViewById(R.id.timer_count_down_id);
        }

        public void bind(final ArrayList<TableInfo> tableList, final int position, final OnItemClickListener listener) {
            TableInfo item = tableList.get(position);
            this.tableList = tableList;
            this.position = position;
            textView.setText(item.getTableNumber());
            if (item.getIsProcessing()) {
                showLoadingImage.setVisibility(View.VISIBLE);
                successTick.setVisibility(View.GONE);
                tableIcon.setVisibility(View.GONE);
                processingLayout.setVisibility(View.VISIBLE);
                transactionAmountTextView.setText(Utils.setFormattedText(String.valueOf(item.getTransactionAmt())));

                synchronized (lstHolders) {
                    updateTimeRemaining();
                }

            } else {
                showLoadingImage.setVisibility(View.GONE);
                tableIcon.setVisibility(View.VISIBLE);
                processingLayout.setVisibility(View.GONE);
            }

            setLayout(tableList, position, item.getIsProcessing(), item.isLastTransactionSuccess());

        }

        public void setLayout(final ArrayList<TableInfo> tableList, final int position, boolean isTransaction, boolean isSuccess) {
            final TableInfo item = tableList.get(position);
            if(isTransaction){
                item.setHandler(null);
                item.setUIChangeCompletedSuccess(true);
                showLoadingImage.setVisibility(View.VISIBLE);
                tableIcon.setVisibility(View.GONE);
                processingLayout.setVisibility(View.VISIBLE);
                transactionAmountTextView.setText(Utils.setFormattedText(String.valueOf(item.getTransactionAmt())));
                lineView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.txn_view_drawable));
            }else{
                showLoadingImage.setVisibility(View.GONE);
                tableIcon.setVisibility(View.VISIBLE);
                processingLayout.setVisibility(View.GONE);

                if(item.isUIChangeCompletedSuccess()) {
                    item.setUIChangeCompletedSuccess(false);

                    if (isSuccess) {
                        successTick.setVisibility(View.VISIBLE);
                        lstHolders.get(position).lineView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.txn_success_view_drawable));
                        Handler handler;
                        if (item.getHandler() == null) {
                            handler = new Handler();
                            item.setHandler(handler);
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    lstHolders.get(position).lineView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.txn_view_drawable));
                                    successTick.setVisibility(View.GONE);
                                    item.setHandler(null);
                                }
                            }, 25000);
                        }
                    } else {
                        successTick.setVisibility(View.GONE);
                        lstHolders.get(0).lineView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.txn_unsuccess_view_drawable));
                        Handler handler;
                        if (item.getHandler() == null) {
                            handler = new Handler();
                            item.setHandler(handler);
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    lstHolders.get(position).lineView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.txn_view_drawable));
                                    successTick.setVisibility(View.GONE);
                                    item.setHandler(null);
                                }
                            }, 25000);
                        }
                    }
                }
            }
        }

        public void updateTimeRemaining() {
            timerCountTextView.setText(preferences.getString(tableList.get(position).getTableId(), ""));
        }
    }
}

我已经设置了这样的适配器:

mAdapter = new TableTransactionRecyclerView(this, tableList, this);
            RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 4, GridLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(mAdapter);

我也在以下链接中查看了问题,但没有解决我的问题。

Recyclerview changing items when is scrolled

Recyclerview Changing Items During Scroll

1 个答案:

答案 0 :(得分:0)

尝试覆盖以下内容:

@Override
public int getItemViewType(int position) {    
    return position;
}