Search code examples
android-recyclerviewxamarin.android

Xamarin.Android Recycler View on click event


I have a recycler view in my application and when the user clicks an item it opens a pop up menu. The user can change some values such as price quantity etc.

The problem occurs when I click the save button it closes the pop up menu but does not show the updated item.

I have set viewstates.visible for the items in the view holder that I want to become visible but when I clic the save button the updated item appears very briefly in a flash but then the item reverts to how it was previously?

If I then go back into the menu to update the qty and price again and press save it appears? But does not appear the first time.

I can provide code when I'm home but just wanted to get people's views as to why this may be happening?

I have set 2 textviews in the recycler view layout and upon creation of the recycler view these 2 textviews visibility is set to gone.

Once the save button is pressed I set the viewstates to visible but this problem occurs?


Solution

  • Do you want to achieve the result like following GIF?

    enter image description here

    First of all, If you want to update the item in recycleview, you should use mAdapter.NotifyItemChanged(e); method to update.

    Based on your description, you push a custom Dialog, change the value in this Dialog, so you Dialog must return value when you change the some item value. So, you should make an event which returns the value when you dismiss the dialog

    1. you would need to add something like:
     public class DialogEventArgs : EventArgs
        {
            public MyModel MyModel { get; set; }
        }
     public delegate void DialogEventHandler(object sender, DialogEventArgs args);
    

    and then in your dialog add:

    public event DialogEventHandler Dismissed;
    

    Then in BtnSaveClick add:

      save.Click += (e, a) =>
                {
                    myModel.Name = editText.Text;
    
                    if (null != Dismissed)
                        Dismissed(this, new DialogEventArgs { MyModel = myModel });
    
                    Dismiss();
    
                };
    

    Then where you create the dialog, which is probably where you want the value you subscribe to the Dismissed event:

      private void MAdapter_ItemClick(object sender, int e)
            {
                //push a Dialog
                var customDialog =  new CustomDialog(this, myModels[e]);
                customDialog.Show();
    
                customDialog.Dismissed += (s, vee) => {
                    /* Update the recycleView */
                    myModels[e]= vee.MyModel;
                    mAdapter.NotifyItemChanged(e);
                };
    
            }
    

    Here is my demo, you can download it and test it.

    https://github.com/851265601/XAndroidRecycleViewUpdateOrClick