There is a recyclerView list with some values; when you switch to one of the elements открывается information about this element открывается . How to make sure that after the element is opened, it changes color in the list.

 public class TopStoriesAdapter extends RecyclerView.Adapter<TopStoriesAdapter.ViewHolder> { private List<Model> topStoriesList; private Context context; public TopStoriesAdapter(List<Model> topStoriesList, Context context) { this.context = context; this.topStoriesList = topStoriesList; } @Override public TopStoriesAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); return new TopStoriesAdapter.ViewHolder(itemView); } @Override public void onBindViewHolder(final TopStoriesAdapter.ViewHolder viewHolder, int i) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); viewHolder.storyTitle.setText(topStoriesList.get(i).getTitle()); viewHolder.storyCreator.setText(topStoriesList.get(i).getBy()); viewHolder.view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context, TopStoryDetailActivity.class); intent.putExtra("key", topStoriesList.get(viewHolder.getAdapterPosition())); context.startActivity(intent); } }); } @Override public int getItemCount() { return topStoriesList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.storyTitle) TextView storyTitle; @BindView(R.id.storyCreator) TextView storyCreator; public ViewHolder(View view) { super(view); storyTitle = (TextView) view.findViewById(R.id.storyTitle); storyCreator = (TextView) view.findViewById(R.id.storyCreator); this.view = view; } } } 

I found a similar example , but I can’t use it for my adapter.

  • one
    There is no need to change the question after having received an answer to it, otherwise the answer loses its meaning and will be useless for others with a similar problem. If you have a new question, ask it alone - pavlofff
  • And it seems in your other question I warned you that it will be so and how it is solved - pavlofff
  • one
    The @pavlofff title of the question has not changed, just because this issue required the division into two subtasks, I had to change it. If you think that you need to edit the question, for the answer you received, with my implementation already - no problem, it's not difficult for me. - Morozov
  • 2
    The fact is that this is not a forum, but a Q & A, here the question is not to help you personally and what kind of "developments" with the accompanying edits, but to solve one specific problem. You had a problem - how to select a read element, you were offered an option. Everything. May offer other options, how to select read. Now you write about a completely different problem - only one element stands out. This is a completely different problem and other answers to it, so ask another question, and this does not require any "development". Also read the rules of this resource - pavlofff
  • one
    I can also tell you right away that the use of preferences is a dead end to solve your problem, do not waste time on it. You were offered a working solution in response - the database, model and field of reading, develop it. With preferences it will not work normally. - pavlofff

2 answers 2

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

  • An excellent answer, but you could add an example of the transition logic for the Shared Preference and add in the comments what is possible with the database. - Morozov
  • I already add;) Wait - Flippy
  • @Morozov The contents of your list does not change during the operation of the application (the data in the list is not added \ deleted)? Otherwise, this whole algorithm will not work. For example, one item is added to the list in the first position. In your preferences it is preserved that the first position is marked, but there will already be another element that has not yet been opened. Similar problem . In order to work correctly with dynamic data, they must be linked in the structure of the data itself, and not according to the positions stored separately. Through preferences it is impossible to implement such a crutch - pavlofff
  • one
    I know it. But the data is not dynamic. Probably. The question does not indicate whether they are static or dynamic - Flippy

You need to somehow identify each item of data. For example by ID.

Then, at the moment that you consider changing the status from “unread” to “read”, you need to somehow save this status or change it.

For example, you can write the ID of the read element to SharedPrefernce. And even better - if you have everything stored in the database and the model has a "read" field that you change. Even better - all data is stored on the server and you change the status of the "read" on the server, and then update the client data.

  • Thank you, but I do not use DB. It would be super as to turn with SharedPreference, until I really saw) - Morozov
  • @Morozov, when reading, write the ID in the preferences. In onBindViewHolder, check if there is a value for this ID. If yes - change the color - YuriySPb
  • @Morozov, SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context) hereinafter pref.contains("key) write pref.edit (). PutString (idInString) .apply (). I don't know how else to explain and what to show. Try a separate ask a question .... - YuriySPb
  • updated the question. Please have a look. - Morozov
  • @Morozov, so I wrote you almost all the necessary code. There are no attempts to apply it in the question ... You need to write to preferences by clicking and in onBindViewHolder to check if there is an entry in preferences and to change the color depending on it. - Yuriy SPb