0

I have a recyclerview to show tables at restaurant, if a table has an active order then I'm setting the background color to Amber. Moreover, if I click on any table, it's background should become grey (I'm using setSelected for this) It works fine for some items but for some, the recyler item's background becomes Amber even though it doesn't have an ActiveOrder. It happens for the tables near a table which actually has an active order. I'm not sure why this is happening.

OnBindViewMethod:

public void onBindViewHolder(TableItemViewHolder holder, int position) {
    Table table = mTableList.get(position);
    holder.mTableNameTextView.setText(table.getTableName());
    holder.tableItemLayout.setSelected(selectedPosition==position);
    if(table.getOrderID()!=0 && selectedPosition!=position)
    {
        Log.i("OrderId",table.getTableName()+table.getOrderID());
        holder.tableItemLayout.setBackgroundColor(mContext.getResources().getColor(R.color.colorAmber));
    }
    holder.tableItemLayout.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyItemChanged(selectedPosition);
                    selectedPosition = position;
                    notifyItemChanged(selectedPosition);
                }
            }
    );
}
Banana
  • 2,435
  • 7
  • 34
  • 60
shubham gupta
  • 321
  • 3
  • 9
  • 23
  • Would it be possible that callling `notifyItemChanged(selectedPosition)` with the previous and the actual `position` value has something to do with the issue? – Chisko Jan 30 '18 at 00:19

1 Answers1

0

I think you need to reset the back ground colour in else part,

public void onBindViewHolder(TableItemViewHolder holder, int position) {
    Table table = mTableList.get(position);
    holder.mTableNameTextView.setText(table.getTableName());
    holder.tableItemLayout.setSelected(selectedPosition==position);
    if(table.getOrderID()!=0 && selectedPosition!=position)
    {
        Log.i("OrderId",table.getTableName()+table.getOrderID());
        holder.tableItemLayout.setBackgroundColor(mContext.getResources().getColor(R.color.colorAmber));
    }else{
// Reset background color
}
    holder.tableItemLayout.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyItemChanged(selectedPosition);
                    selectedPosition = position;
                    notifyItemChanged(selectedPosition);
                }
            }
    );
}
Rudrik Patel
  • 676
  • 6
  • 13
  • It worked with slight modification thanks!! 1st if block to set color amber if any active order is there , 2nd if block to set background to grey if it is selected condition being(selectedPosition==position) and a else block to reset the background color – shubham gupta Jan 30 '18 at 14:48