title: How to move item to the last position when Checkbox is checked? (RecyclerView, ViewHolder)
body: <p><a href="https://i.stack.imgur.com/t5Q9m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t5Q9m.png" alt="enter image description here"></a></p>
  
  <p>I try to make a RecyclerView who contains checkBoxes (if something is already bought). I want to move item to the last position on CheckBox checked change and set Background to green. I tried to remove item from ArrayList and then add it again but it works only with String name, it doesn't keep state (checkbox state or color). Other times it throws Exception: Cannot call this method while RecyclerView is computing a layout or scrolling. Can somebody tells me what i am doing wrong?</p>
  
  <p>Sry for my english.</p>
  
  <pre><code>import android.graphics.Color;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.helper.ItemTouchHelper;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import java.util.ArrayList;
  
  public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; 
  {
  private ArrayList&lt;String&gt; mDataset;
  private ArrayList&lt;FinalListItem&gt; finalListItems;
  public MyAdapter(ArrayList&lt;String&gt; mDataset) {
  this.mDataset = mDataset;
  [enter image description here][1]finalListItems = new ArrayList&lt;&gt;();
      for (String a : mDataset) {
          finalListItems.add(new FinalListItem(a, false));
      }
  }
  @Override
  public long getItemId(int position) {
      return position;
  }
  @Override
  public int getItemViewType(int position) {
      return position;
  }
  @Override
  public void onBindViewHolder(final MyViewHolder holder, final int position) {
      holder.final_list_TextView1.setText(finalListItems.get(position).getName());
      if(finalListItems.get(position).isChecked){
          holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
      }
      else{
          holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
      }
      holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              FinalListItem finalItem = new FinalListItem(finalListItems.get(position).getName(),true);
              if (isChecked) {
                  holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
                  finalListItems.remove(finalListItems.get(position));
                  finalListItems.add(finalItem);
                  notifyDataSetChanged(); //weird behaviour of app
              } else {
                  holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
              }
  
          }
      });
  }
  @Override
  public int getItemCount() {
      return finalListItems.size();
  }
  
  private class FinalListItem {
      private String name;
      private boolean isChecked;
  
      public FinalListItem(String name, boolean isChecked) {
          this.name = name;
          this.isChecked = isChecked;
      }
  
      public boolean isChecked() {
          return isChecked;
      }
  
      public void setChecked(boolean isChecked) {
          this.isChecked = isChecked;
      }
  
      public String getName() {
          return name;
      }
  }
  
  public class MyViewHolder extends RecyclerView.ViewHolder {
      final public TextView final_list_TextView1;
      final public CheckBox checkBox;
      final public LinearLayout linearLayout;
  
      public MyViewHolder(View view) {
          super(view);
          final_list_TextView1 = (TextView) view.findViewById(R.id.final_list_TextView1);
          checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
          linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout1);
      }
  }
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.finallist_list_layout, parent, false);
      return new MyViewHolder(view);
  }
  </code></pre>
  
  <p>}</p>