-1

I have a recyclerview where i want to change the color of the selected item and re change it on unselected. I have used an string arraylist and an interface for that here is interface code in adapter -

  public interface Callback{
        void onItemClicked(String i_name, boolean longClick);
    }

Here is onclick and onlongclick code-

 @Override
    public void onClick(View view) {
           String[] tag = ((String) view.getTag()).split(":");
           String i_name = tag[1];
        Toast.makeText(context, ""+i_name, Toast.LENGTH_SHORT).show();
        if(callback != null)
        {
            callback.onItemClicked(i_name,false);
        }

    }

    @Override
    public boolean onLongClick(View view) {
        String[] tag = ((String) view.getTag()).split(":");
        String i_name = tag[1];
        if(callback != null)
        {
            callback.onItemClicked(i_name,false);
        }
        return false;

    }

Here is the toggleselected code -

  public void toggleSelected(String i_name)
    {
        final boolean newState = !selectedList.contains(i_name);
        if(newState)
        {
         // i want to give background color to i_name

               selectedList.add(i_name);
               Toast.makeText(context, "selected list1- "+selectedList, Toast.LENGTH_SHORT).show();
        }
        else
        {
            selectedList.remove((String) i_name);
            Toast.makeText(context, "selected list2- "+selectedList, Toast.LENGTH_SHORT).show();

        }
        notifyDataSetChanged();
    }

here is onItemClicked code from fragment -

 @Override
    public void onItemClicked(String i_name, boolean longClick) {
        if(longClick)
        {
             ((MyCategoryAdaptercheckbox) MyAdapter).toggleSelected(i_name);
        }
        else
        {
            ((MyCategoryAdaptercheckbox) MyAdapter).toggleSelected(i_name);
        }
    }

This is the code from onbindviewholder where i am settingthe tag -

 getMyCategoryAdapter1 =  category_name.get(i);

        viewHolder.view.setActivated(selectedList.contains(i));
        viewHolder.view.setTag("items:" + getMyCategoryAdapter1.getC_name());
        viewHolder.view.setOnClickListener(this);
        viewHolder.view.setOnLongClickListener(this); 

I want to give background color to i_name on toggleselected() method.HOw can i do this.Please help.Thanks in advance.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
payal_suthar
  • 355
  • 8
  • 31
  • check this https://stackoverflow.com/questions/54103473/how-to-clear-highlighted-items-in-the-recyclerview/54103578#54103578 – AskNilesh Apr 12 '19 at 07:57
  • Why did you make the code so complicated?? – Piyush Apr 12 '19 at 07:58
  • @Piyush Are you talking about using xml to change color on item click – Ranjit Apr 12 '19 at 08:09
  • @Piyush..I am new to andriod and couldn't think of a much easier way.can you help? – payal_suthar Apr 12 '19 at 08:11
  • @Ranjit no i want to change the background color problematically only, but how do i do that? I am not able to get the view on toggleselected method so that i can change the color of it. – payal_suthar Apr 12 '19 at 08:12
  • @Piyush there are easier ways but i have a search feature in my code also where the positions of the items get updated o i cant rely on positions. – payal_suthar Apr 12 '19 at 08:13
  • @payal_suthar follow this approach https://stackoverflow.com/questions/40692214/changing-background-color-of-selected-item-in-recyclerview – Ranjit Apr 12 '19 at 08:56

1 Answers1

0

Basic idea for selecting and disselecting items in a recyclerview.

Maintain a tag in your model for selected and unselected.

for e.x

boolean isSelected;

when you populate the data make all values in your list for isSelected false bydefault.

Then in your long press set the value of isSelected to true for that position only in your arraylist and call notifyDataSetChanged.

and in your onbindviewholder check

if(yourModel.isSelected){

// show the row selected

}else {

// show the row unselected

}

I hope you get the idea.

AbhayBohra
  • 2,047
  • 24
  • 36
  • Ya but here in my code, i am updating the boolean variable newState on toggle selected but how do i get the view id of the particular string i_name? – payal_suthar Apr 12 '19 at 08:21