question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Avoiding RecyclerView ViewHolder Events During Notify

I have a RecyclerView whose ViewHolder contains elements with events (such as the TextChanged event of EditText). I have a point in my code in which I want to clear the item list & add new items (sort of start over). However, during this process the TextChanged event is being triggered for EditText(s) in ViewHolder I no longer need (that should just be used after being recycled). When I override OnViewRecycled, I detach these event handlers, but that doesn't seem to be helping. How can I prevent or detach events when calling NotifyDataSetChanged()?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

However, during this process the TextChanged event is being triggered for EditText(s) in ViewHolder

Hi, njsokalski. I created a basic demo to test the function, the TextChanged event will not be triggered when clearing and adding new items. Here is the code of my project, please check it. If it doesn't work on your side, please share the related code to reproduce the issue.

private async void MainActivity_Click(object sender, EventArgs e)
{
    list.Clear();
    list.Add(new model { ... });
    ...
    myadapter.NotifyDataSetChanged();
}

Custom ViewHolder class

public class CustomViewHolder : RecyclerView.ViewHolder
{
    public EditText Edit { get; private set; }

    public _ViewHolder(View itemView) : base(itemView)
    {
        Edit = itemView.FindViewById<EditText>(Resource.Id.edit);
        Edit.TextChanged += delegate
        {
            //
        };
    }
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the TextChanged event will not be triggered when clearing and adding new items

That's what I thought was supposed to happen, but the EditText events (TextChanged & FocusChange) are still being triggered. Here is my code:

 this.NewPlayers.Clear();
 this.NewPlayers.AddRange((MainActivity.Players == null) ? new List<PlayerData>() { new PlayerData() } : MainActivity.Players.Append(new PlayerData()));
 this.rvPlayerNamesInput.GetAdapter().NotifyDataSetChanged();

The only difference I can see between my code and yours is that I add the handlers in OnBindViewHolder (and remove them in OnViewRecycled) in the Adapter, while you add them in the ViewHolder. Is it possible that this is causing a problem?

0 Votes 0 ·

The only difference I can see between my code and yours is that I add the handlers in OnBindViewHolder

I mistakenly thought you added this event in the custom ViewHolder class. I moved it to the custom Adapter class and modified the code as you described, the TextChanged still was not triggered. Here is the related code:

public class CustomAdapter : RecyclerView.Adapter
{
    ...
    public override void OnViewRecycled(Java.Lang.Object holder)
    {
        base.OnViewRecycled(holder);

        CustomViewHolder _vh = holder as CustomViewHolder;
        _vh.Edit.TextChanged -= Edit_TextChanged;
    }
    CustomViewHolder vh;
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        CustomViewHolder vh = holder as CustomViewHolder;
        vh.Edit.TextChanged += Edit_TextChanged;
    }
    private void Edit_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
    {
        //
    }
}
0 Votes 0 ·