1

我正在尝试使用 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 库是这个

非常感谢!

4

1 回答 1

0

问题是在你刷了项目之后,UI 中的项目消失了,适配器中的项目。但是您的列表中有一个空白区域,这就是您有一个空白区域的原因。

我假设您在活动和适配器中有这些对象的列表。

使用从适配器列表中删除项目

list.remove(position);

然后在你的适配器中有一个方法

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

这段代码在java中。所以请把它转换成 Xamarin。

最后,在滑动时在回调中调用此方法

adapter.updateList(list);

我希望这有帮助。

于 2018-02-20T04:43:55.053 回答