1

I was experimenting with the recyclerview trying to learn how to allow a swipe dismiss with an item in the view. Whenever I dismiss an item, the space the item used to take up is still there with an empty space. The items won't move up to populate the empty space.

Screenshot of empty space

Is there something I am missing? (I am using Xamarin, but the solution, I assume, is practically the same as if I were using java)

Here is the Recyclerview adapter with eh ItemTouchHelperAdapter

public class NoteAdapter : RecyclerView.Adapter, IItemTouchHelperAdapter
{
    public NoteObject[] notes;
    public Context c;

    public override int ItemCount => notes.Length;
    public override long GetItemId(int position) => position;

    public class NoteViewHolder : RecyclerView.ViewHolder
    {
        public View MainView { get; set; }
        public CardView MainCView { get; set; }
        public TextView TitleView { get; set; }
        public TextView MessageView { get; set; }
        public NoteViewHolder(View v) : base(v) { }
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View noteCard = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.NoteCardView, parent, false);
        CardView cView = (CardView)noteCard.FindViewById(Resource.Id.noteCard);
        TextView titleView = (TextView)noteCard.FindViewById(Resource.Id.noteTitle);
        TextView messageView = (TextView)noteCard.FindViewById(Resource.Id.noteMessage);

        return new NoteViewHolder(noteCard) { TitleView = titleView, MessageView = messageView, MainCView = cView };
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        NoteViewHolder noteHolder = (NoteViewHolder)holder;

        noteHolder.MainCView.Click += (sender, e) => 
        {
            Intent i = new Intent(c, typeof(NoteReview));
            Bundle b = new Bundle();
            b.PutString(c.GetString(Resource.String.noteBundleKey), notes[position].ToString()); 
            b.PutInt(c.GetString(Resource.String.noteIDBundleKey), position);
            i.PutExtra(c.GetString(Resource.String.intentKey), b);
            c.StartActivity(i);
        };

        noteHolder.MainCView.LongClick += (sender, e) =>
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(c);
            alert.SetTitle(Resource.String.dialogTitle);

            alert.SetPositiveButton(Resource.String.delete, (dSender, dE) => { Firebase.DeleteAt(position); });
            alert.SetNeutralButton(Resource.String.cancel, (dSender, dE) => {});

            Dialog d = alert.Create();
            d.Window.SetType(WindowManagerTypes.SystemAlert);

            MainActivity.StartDialog(d);
        };

        noteHolder.TitleView.Text = notes[position].Title;
        noteHolder.MessageView.Text = notes[position].Message;
    }

    public bool OnItemMove(int fromPosition, int toPosition) => true;

    public void OnItemDismiss(int position)
    {
        Firebase.DeleteAt(position);
        notes = Firebase.GetNotes();
        NotifyItemRemoved(position);
    }
}

Here is the callback

    public class NoteSwipeHandler : SimpleItemTouchHelperCallback
{
    IItemTouchHelperAdapter recAdapter { get; set; }

    public NoteSwipeHandler(IItemTouchHelperAdapter adapter) : base(adapter)
    {
        recAdapter = adapter;
    }

    public override bool IsLongPressDragEnabled => true;
    public override bool IsItemViewSwipeEnabled => true;

    public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target)
    {
        recAdapter.OnItemMove(source.AdapterPosition, target.AdapterPosition);
        return true;
    }

    public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int i)
    {
        recAdapter.OnItemDismiss(viewHolder.AdapterPosition);
    }
}

The Xamarin library I'm using for the item touch helper is this

Thanks a bunch!

FreakyAli
  • 13,349
  • 3
  • 23
  • 63

1 Answers1

0

The thing is after you swiped the item, the item from the UI is gone and the item in adapter. But there is a vacant space in your list and that's why you have a blank space.

I assume you have a list of those objects in the activity and in the adapter.

Remove the item from the list in adapter using

list.remove(position);

Then have a method in your adapter

public void updateList(List<Lists> lists){
this.lists = lists;
notifyItemSetChanged();
}

This code is in java. So please convert it into Xamarin.

And finally, call this method in the callback when swiped

adapter.updateList(list);

I hope this helps.

Sriram R
  • 2,109
  • 3
  • 23
  • 40