Let's look at the problem from the other side. In this example adapter, we set up an array of boolean values; when we click on a list item in the array, the value changes to true . In the onBindViewHolder method, the background with a red / transparent color is set depending on true/false .
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.AdapterHolder> { List<Model> names; boolean[] checked; int check_count = 0; public class AdapterHolder extends RecyclerView.ViewHolder { RelativeLayout tbrvitemroot; AdapterHolder(View itemView) { super(itemView); tbrvitemroot = (RelativeLayout)itemView.findViewById(R.id.tbrvitemroot); } } RecyclerViewAdapter(List<Model> names){ names = names; checked = new boolean[names.size()]; } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public AdapterHolder onCreateViewHolder(ViewGroup viewGroup, int p2) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tbrvitem, viewGroup, false); AdapterHolder pvh = new AdapterHolder(v); return pvh; } @Override public void onBindViewHolder(final AdapterHolder holder, final int position) { if(checked[position]) holder.tbrvitemroot.setBackgroundColor(Color.RED); else holder.tbrvitemroot.setBackgroundColor(Color.TRANSPARENT); holder.tbrvitemroot.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { checked[position] = true; //тут логика перехода } }); } @Override public int getItemCount() { return names.size(); } }
Now that everything has been disassembled, we go further. Since it is necessary to show the reads on subsequent launches of the application, we need to replace the array logic with the logic of the stored values. There are two options - either a database or Preferences . We will use the second, because it is simpler and a little data. Now we will think in what form we will store the data.
Не прочитано - 0 Прочитано - 1
That is, if there are 10 elements in total, and the 2nd, 5th and 7th are read, then in the Preferences will be
0010010100
Yes exactly. After all, we do not store positions, but indexes. Now the implementation itself
1) Before creating an adapter, you need to get data
boolean[] checked; SharedPreferences mSettings = getSharedPreferences("save_state", Context.MODE_PRIVATE); if (mSettings.contains("save")) String saved_str = mSettings.getString("save", "")); for(int x = 0; x<saved_str.length; x++){ String saved_char = saved_str.substring(x, x+1); if(saved_char.equals("0")) checked[x] = false; else checked[x] = true; } else checked = new boolean[ /*здесь длина массива*/ ];
2) Send the array along with the data to the adapter
RecyclerViewAdapter rva = new RecyclerViewAdapter(names, checked); rv.setAdapter(rva);
3) Adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.AdapterHolder> { List<Model> names; boolean[] checked; int check_count = 0; public class AdapterHolder extends RecyclerView.ViewHolder { RelativeLayout tbrvitemroot; AdapterHolder(View itemView) { super(itemView); tbrvitemroot = (RelativeLayout)itemView.findViewById(R.id.tbrvitemroot); } } RecyclerViewAdapter(List<Model> names){ names = names; checked = new boolean[names.size()]; } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public AdapterHolder onCreateViewHolder(ViewGroup viewGroup, int p2) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tbrvitem, viewGroup, false); AdapterHolder pvh = new AdapterHolder(v); return pvh; } @Override public void onBindViewHolder(final AdapterHolder holder, final int position) { if(checked[position]) holder.tbrvitemroot.setBackgroundColor(Color.RED); else holder.tbrvitemroot.setBackgroundColor(Color.TRANSPARENT); holder.tbrvitemroot.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { holder.tbrvitemroot. setBackgroundColor(Color.RED); StringBuilder sb = new StringBuilder(); for(int x = 0; x<names.size(); x++){ String new_char = (checked[x]) ? "1" : "0"; sb.append(new_char); } Editor editor = mSettings.edit(); editor.putString("save", sb.toString();); editor.apply(); //тут логика перехода } }); } @Override public int getItemCount() { return names.size(); } }
Here we take data from Preferences (if we don’t have it, we don’t take it), "decrypt" the string into an array of boolean values and sent to the adapter. The red / transparent logic remains the same as in the adapter above. When you click on the list item, you had to completely update the row and data.
A few words about the transition
Everything turned out so well that when you go back to the activity with the list, nothing is lost. You can return either with Intent or finish() , the only difference is in the loss of a scroll in the first case.
Moral: THINK AS AN ADAPTER