我创建了一个类来处理滑动事件:

public class CardViewSwipeListener : ItemTouchHelper.Callback
{
   private Context context;
   private List<Person> listWithData;
   private const int RESET_ADAPTER = 1;

    public CardViewSwipeListener(Context context, List<Person> listWithData)
    {
        this.context = context;
        this.listWithData = listWithData;
    }

    public override int GetMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
    {
        int swipeFlags = ItemTouchHelper.Start | ItemTouchHelper.End;
        return MakeMovementFlags(0, swipeFlags);
    }

    public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
    {
        return true;
    }

    public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
    {
        if (direction == ItemTouchHelper.End)
        {
            ((Activity)context).StartActivityForResult(new Intent(Intent.ActionCall, Android.Net.Uri.Parse(string.Format("tel:{0}", listWithData[viewHolder.AdapterPosition].PhoneNumber))),RESET_ADAPTER);
            Toast.MakeText(context,"Call "+listWithData[viewHolder.AdapterPosition].Name,ToastLength.Short).Show();
        }
        else if (direction == ItemTouchHelper.Start)
        {
            ((Activity)context).StartActivityForResult(new Intent(Intent.ActionView, Android.Net.Uri.Parse(string.Format("sms:{0}", this.listWithData[viewHolder.AdapterPosition].PhoneNumber))),RESET_ADAPTER;
            Toast.MakeText(context, "SMS " + this.listWithData[viewHolder.AdapterPosition].Name, ToastLength.Short).Show();
        }
    }
}


在MainActivivty onCreate中,我使用以下代码提供回收程序视图来处理滑动事件。

        itemTouchHelper   = new ItemTouchHelper(new CardViewSwipeListener(this, listWithData));

        recycler = FindViewById<RecyclerView>(Resource.Id.recycler);
        recycler.HasFixedSize = true;
        layoutManager = new LinearLayoutManager(this);
        recycler.SetLayoutManager(layoutManager);
        recycler.SetAdapter(new RecyclerViewAdapter(listWithData,this));
        itemTouchHelper.AttachToRecyclerView(recycler);


问题是我需要将listWithData更改为另一个,但是当我这样做时,onSwipe不会考虑任何更改。

没有结果:

itemTouchHelper   = new ItemTouchHelper(new CardViewSwipeListener(this, NewList));
        recycler.SetAdapter(new RecyclerViewAdapter(NewList,this));
        itemTouchHelper.AttachToRecyclerView(recycler);


也接受Java代码解决方案)

最佳答案

问题是我需要将listWithData更改为另一个,但是当我这样做时,onSwipe不会考虑任何更改。


您可以编写Interface来监听ItemTouchHelper.Callback的解雇事件:

public interface IItemHelper
{
    //Called when an item has been dismissed by a swipe.
    void onItemDismiss(int position);
}


在您的IItemHelper类中实现RecyclerViewAdapter接口:

public class MyRecyclerViewAdapter : RecyclerView.Adapter,IItemHelper
{
    List<string> items;

    ...

    public MyRecyclerViewAdapter(List<string> data)
    {
        items = data;
    }

    ...

    //Called when an item has been dismissed by a swipe.
    public void onItemDismiss(int position)
    {
        items.RemoveAt(position);
        NotifyItemRemoved(position);
    }
}


轻扫项目时,告诉适配器关闭该项目:

public class CardViewSwipeListener : ItemTouchHelper.Callback
{
     ...

     public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
    {
        if (direction == ItemTouchHelper.End)
        {
            ...
        }
        else if (direction == ItemTouchHelper.Start)
        {
            ...
        }
        recyclerViewAdapter.onItemDismiss(viewHolder.AdapterPosition);
    }
}


效果类似于this

07-28 13:05