I have followed the answer to this question and tried to highlight the selected entry from the RecyclerView. I am working with the Master Detail Flow Layout and this solution works very well for the landscape mode. In the portrait mode, when I select an entry and go to the second activity (which contains a fragment) and delete it(or make changes and save it), and then I come back to the MainActivity(), that position is still highlighted.
How do I solve this?
Some of my code:
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
int selected_position=-1;
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, final int position) {
// Set Text and Checkbox
if(selected_position == viewHolder.getAdapterPosition()){
viewHolder.itemView.setBackgroundColor(Color.MAGENTA);
}else{
viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onEntrySelected(position);
notifyItemChanged(selected_position);
selected_position = position;
notifyItemChanged(selected_position);
}
});
}
public void dismissItem(int pos) {
selected_position=-1;
mEntries.remove(pos);
notifyDataSetChanged();
}
}