1
\$\begingroup\$

I'm having a ListView which contains some users. This ListView is used to select users which should be added to a new group chat.

When I tap on an element in the list, it feels not good because it doesn't instantly visually update the CheckBox of this element to be checked/unchecked.

Additionally, I have a "Select All" element at the top of the list. If tapped, all users will be selected (or deselected if every user is already selected). This also needs 1-2 seconds until all checkboxes are visually updated.

Can I achieve a better UX here?

ViewModel code:

public void OnUserTapped(UserDto userDto)
{
    if (userDto != null)
    {
        if (userDto.status == "alleListElement")
        {
            OnAlleToggled();
        }
        else
        {
            PageItem.Users.FirstOrDefault(u => u.openId == userDto.openId).isSelected = !userDto.isSelected;
            PageItem.Users = new ObservableCollection<UserDto>(PageItem.Users); // Needed so that the CheckBoxes visually updates
        }
    }
}

public void OnAlleToggled()
{
    bool isToggled = true;
    if (PageItem.Users.FirstOrDefault(u => u.isSelected == false) == null)
    {
        isToggled = false;
    }
    foreach (UserDto user in PageItem.Users)
    {
        user.isSelected = isToggled;
    }

    PageItem.Users = new ObservableCollection<UserDto> (PageItem.Users);
}

I tried to access the ViewCells of the ListView directly. This is very fast and offers a good UX:

private void Handle_OnUserTapped(object sender, EventArgs e)
{
    var grid = sender as Grid;
    var userDto = grid.BindingContext as UserDto;
    //_addParticipantsPageViewModel.OnUserTapped(userDto);

    if (userDto.status == "alleListElement")
    {
        // TODO... Can't access all ViewCells of the ListView because ListView.TemplatedItems is internal
    }
    else
    {
        OneCheckbox checkbox = GetCheckBoxFromGrid(grid.Children) as OneCheckbox;
        checkbox.IsChecked = !checkbox.IsChecked;
    }
}

But I know that it's bad code to access a ViewCell directly and I usually handle stuff like this in the ViewModel. Is this acceptable for the sake of performance? Also, I don't know if it's possible to get access to all ViewCells of the ListView. ListView.TemplatedItems is internal...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.