Android RecyclerView点击项文本状态持久化问题求助
Hey there! The issue you're facing makes total sense—right now you're only modifying the in-memory list, which gets recreated from scratch every time the fragment's onCreateView runs (like when you restart the activity). To keep the state between app restarts, you need to persist that data somewhere. Since you're dealing with small, simple text states, SharedPreferences is perfect for this job.
Step 1: Initialize your list with saved state
Instead of hardcoding "Day" + i every time, pull the saved status from SharedPreferences first. Update your onCreateView method like this:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView= inflater.inflate(R.layout.fragment_main, container, false); recyclerView=(RecyclerView)rootView.findViewById(R.id.item_list); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); // Get SharedPreferences instance to save/load state SharedPreferences prefs = getActivity().getSharedPreferences("DayStatus", Context.MODE_PRIVATE); list=new ArrayList<>(); for(int i=1;i<50;i++){ // Check if we have a saved state for this day String savedStatus = prefs.getString("day_" + i, "Day" + i); list.add(savedStatus); } adapter =new MyListAdapter(getActivity(),list,this); recyclerView.setAdapter(adapter); return rootView; }
Step 2: Save the state when you update an item
When you click to change "Day" to "finished", you need to write that change to SharedPreferences so it sticks. Update your onNoteClick method (and clean up some unnecessary calls):
@Override public void onNoteClick(int position) { if (position == 0) { Log.d(TAG, "onNoteClick: "); // Update the in-memory list list.set(0, "finished"); // Only notify for the specific item (no need for notifyDataSetChanged) adapter.notifyItemChanged(0); // Save the state to SharedPreferences SharedPreferences.Editor editor = getActivity().getSharedPreferences("DayStatus", Context.MODE_PRIVATE).edit(); // Position 0 maps to "Day1" in your list, so use key "day_1" editor.putString("day_" + (position + 1), "finished"); editor.apply(); // Async save (better for UI thread than commit()) // Your existing fragment transaction code Fragment fr = new Day1(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.Mainfragment1, fr,null).addToBackStack(null); fragmentTransaction.commit(); } }
Quick note on position mapping
Your loop starts at i=1 for "Day1", which ends up at position 0 in the list. So when saving, we use position + 1 to match the "day_1" key—this keeps your saved state aligned with the original list naming.
Bonus: Toggle functionality (if needed later)
If you want to let users switch back to "DayX" from "finished" later, you can extend the code like this:
@Override public void onNoteClick(int position) { String dayKey = "day_" + (position + 1); String currentText = list.get(position); if (currentText.equals("finished")) { // Switch back to original "DayX" String originalText = "Day" + (position + 1); list.set(position, originalText); getActivity().getSharedPreferences("DayStatus", Context.MODE_PRIVATE) .edit() .putString(dayKey, originalText) .apply(); } else { // Switch to "finished" list.set(position, "finished"); getActivity().getSharedPreferences("DayStatus", Context.MODE_PRIVATE) .edit() .putString(dayKey, "finished") .apply(); } adapter.notifyItemChanged(position); // Keep your fragment transaction code here if needed }
That's it! Now when you restart the activity, your fragment will load the saved status from SharedPreferences instead of creating a fresh list, so the "finished" text will stay put.
内容的提问来源于stack exchange,提问作者Bata Kos

