gpt4 book ai didi

java - ItemTouchHelper onSwiped() 不更新来自构造函数的数据

转载 作者:行者123 更新时间:2023-11-30 10:27:06 26 4
gpt4 key购买 nike

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

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 中,我使用以下代码提供回收器 View 来处理滑动事件。

        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 代码解决方案)

最佳答案

The problem is that i need to change listWithData to another one, but when i do that onSwipe doesn't consider any changes.

您可以编写一个 Interface 来监听来自 ItemTouchHelper.Callback 的关闭事件:

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

RecyclerViewAdapter 类中实现 IItemHelper 接口(interface):

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 .

关于java - ItemTouchHelper onSwiped() 不更新来自构造函数的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com