0

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();
    }
}

All Screenshots

Community
  • 1
  • 1
codelover
  • 11
  • 5
  • How about having a resetSelectedPos() method in which you set the selected_position to -1 again? and then call notifyDataSetChanged(); This should rerun onBindViewHolder and then fix your issue? – Smashing Nov 18 '16 at 10:37

1 Answers1

0

Set result before finishing your DetailActivity on delete operation. Then you can handle your required action in onActivityResult as :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    selectedPosition = -1;
    mAdapter.notifyDataSetChanged();
}

Because before calling onPause android saves the activity state.

You can checkout the working example here, please pardon me for code quality. MasterDetailStackOverFlow - GitHub

Tasneem
  • 793
  • 8
  • 24