0

As I'm new to android, I'm struggling to highlight Recyclerview clicked or current item. I have tried some workarounds but nothing helps. Basically I want to highlight selected item even after it is coming back from respective Fragment. Please check my code and help me to get done. Thanks.

public class ContentaAdapter extends RecyclerView.Adapter<ContentaAdapter.MyViewHolder>  {

    Context context;

    ArrayList<String> ItemTitle;
    ArrayList<String> ItemSource;

    public ContentaAdapter(Context context, ArrayList<String> ItemTitle, ArrayList<String> ItemSource) {
        this.context = context;
        this.ItemTitle = ItemTitle;
        this.ItemSource = ItemSource;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_items_layout, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.ItemTitle.setText(ItemTitle.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment contentdisplay = new ViewContentFragment();
                Bundle bundle=new Bundle();
                bundle.putStringArrayList("ItemTitle",ItemTitle);
                bundle.putStringArrayList("ItemSource",ItemSource);
                bundle.putInt("position",position);
                bundle.putInt("ItemCounts",ItemTitle.size());
                contentdisplay.setArguments(bundle);
                ((MainActivity)context).replaceFragment(contentdisplay);
            }
        });

    }

    @Override
    public int getItemCount() {
        return ItemTitle.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView ItemTitle;
        public MyViewHolder(View itemView) {
            super(itemView);
            ItemTitle = (TextView) itemView.findViewById(R.id.item_title);
        }
    }
}
ilmk
  • 599
  • 1
  • 9
  • 19
  • First, don't call "replaceFragment" from the Adapter and don't do Android API calls at all from the Adapter, use Listener (Interface) which to pass this event (onItemClick) into the Activity – MrVasilev Nov 06 '19 at 08:00
  • And maybe you can find something helpful here: https://stackoverflow.com/questions/27194044/how-to-properly-highlight-selected-item-on-recyclerview or here: https://stackoverflow.com/questions/28972049/single-selection-in-recyclerview – MrVasilev Nov 06 '19 at 08:04
  • Thanks for your suggestion. I will give a Try !! – ilmk Nov 06 '19 at 08:05

1 Answers1

0

you need to to add isSelected state in to your list item data model and change it onClick.

And when you know state you can change background in ViewHolder

 if (isSelected) {
            //  set selected background here
        } else {
            //  set not selected background here
        }

And instead of keeping two lists in adapter you should create one with model ArrayList<DataModel> similar to this:

class DataModel {
    String ItemTitle;
    String ItemSource;
    Boolean isSelected;
}

also you shouldn't pass both list to other fragment, instead take only what you need, for example yourList.get(position);

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34