我正在尝试使用 recyclerview 尝试学习如何允许滑动关闭视图中的项目。每当我关闭一个项目时,该项目过去占用的空间仍然存在,并且有一个空白空间。这些项目不会向上移动以填充空白空间。
有什么我想念的吗?(我正在使用 Xamarin,但我认为该解决方案实际上与我使用 java 相同)
这是带有 eh ItemTouchHelperAdapter 的 Recyclerview 适配器
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);
}
}
这是回调
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);
}
}
我用于项目触摸助手的 Xamarin 库是这个
非常感谢!