How to multi select for cards in recycler view

As per the Image I have written a recycler view and in Frame layout with cardview I have given the code to get the images and text. Now how to make that checkmark icon appeared only when user selects that particular holiday.

Tagged:

Answers

  • subramanyamnuggusubramanyamnuggu Member ✭✭

    And one more how can I get that border color as blue when selected on the particular view

  • JarvanJarvan Member, Xamarin Team Xamurai

    how to make that checkmark icon appeared only when user selects that particular holiday ... get that border color as blue when selected on the particular view

    Add an IsSelected property in your model class, then set the IsSelected property to true when item is being selected. Detect the IsSelected property to change the item's status.

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        CustomViewHolder viewHolder = holder as CustomViewHolder;
        ...
    
        viewHolder.ParentLayout.SetBackgroundColor(models[position].IsSelected ? Android.Graphics.Color.Gray : Android.Graphics.Color.White);
    }
    

    Refer to:
    https://stackoverflow.com/questions/49610629/multiselect-recyclerview

  • subramanyamnuggusubramanyamnuggu Member ✭✭

    Hope my question was not that perfect, Sorry for that

    The problem statement is I have to display a list of holidays as shown in the card which the names I will get from API. The API will give me the list of Holidays and the holidays which user already opted and the holidays which are passed by. So as shown in the picture the first one represent that the user applied previously and the date is passed , The second one is he didn't apply previously but the date has been passed by. The third one he selected not so the border changes to blue and the green checkmark icon enables. Hope you can see the difference between capture2 and capture images.

    I am a beginner to xamarin.Please suggest me some solution.

  • subramanyamnuggusubramanyamnuggu Member ✭✭

    Capture2 is first pic and capture is second pic

  • JarvanJarvan Member, Xamarin Team Xamurai

    Add an IsSelected property and detect the IsSelected's value to display the icon. Use the same method to set the backgroundColor and border color

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        CustomViewHolder viewHolder = holder as CustomViewHolder;
        viewHolder.text.Text = models[position].content;
        viewHolder.ItemView.Tag = position;
        viewHolder.ItemView.SetOnClickListener(this);
    
    
        viewHolder.image.Visibility = models[position].IsSelected ? ViewStates.Visible : ViewStates.Invisible;
    }
    
    public void OnClick(View v)
    {
        int position = (int)v.Tag;
        models[position].IsSelected = !models[position].IsSelected;
        NotifyDataSetChanged();
    }
    

  • subramanyamnuggusubramanyamnuggu Member ✭✭

    Can you share me the project which you created for Multiselect

  • subramanyamnuggusubramanyamnuggu Member ✭✭

    I am also getting this Error
    cannot convert from ViewController.RecyclerAdapter to Android.View.View.OnclickListener
    So can me please share me the project it will be more helpful for me

  • JarvanJarvan Member, Xamarin Team Xamurai

    Here is the demo, you can check the code.

  • subramanyamnuggusubramanyamnuggu Member ✭✭
    edited September 6

    Hi Jarvan,

    I am getting the data from API which the user had already availed leaves and the leaves which he didn't availed. Now how can I make the card disabled and change its background color if the user already availed the leave. I the picture Good Fraiday has been already aviled by the user so the color is grey and the card is disabled and there is checkmark icon since he availed before, and next Eid Ul Fitr is disabled since he can't avail now because the date is passed by and there is nocheckmark icon since he didnt avail. Please help me how to achieve this.

  • subramanyamnuggusubramanyamnuggu Member ✭✭
    edited September 6

    For Good Friday there is a checkmark Icon since the user availed, But there is no checkmark icon fror Eid UL Fitr since the user didnt avail,

  • JarvanJarvan Member, Xamarin Team Xamurai

    Now how can I make the card disabled and change its background color if the user already availed the leave.

    You need to add another property in model class like IsSelected to note if the user already availed the leave.

Sign In or Register to comment.