解雇物品后{convert回复物品'之间的差距

时间:2018-02-19 20:23:24

标签: android android-recyclerview xamarin.android recyclerview-layout itemtouchhelper

我正在尝试使用recyclerview尝试学习如何允许使用视图中的项目轻扫。每当我解雇一个项目时,该项目过去占用的空间仍然是空的空间。这些物品不会向上移动以填充空白区域。

Screenshot of empty space

我有什么遗失的吗? (我正在使用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库是this

非常感谢!

1 个答案:

答案 0 :(得分:0)

事情是在您刷完项目后,UI中的项目消失了,适配器中的项目。但是你的清单中有空位,这就是你有空白的原因。

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

使用

从适配器列表中删除项目
list.remove(position);

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

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

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

最后,在刷卡时调用此方法

adapter.updateList(list);

我希望这会有所帮助。