gpt4 book ai didi

Android Button Click Event 在实现带有搜索过滤器的 recyclerview 时被多次触发?

转载 作者:太空狗 更新时间:2023-10-29 13:07:09 25 4
gpt4 key购买 nike

我在 Xamarin Android 中工作。我已经实现了带有患者列表的回收者 View 。我还实现了一个搜索 View 来过滤搜索到的患者列表。我在 OnBindViewHolder 中有一个按钮单击事件,每当我在列表更新时实现搜索时,它都会被多次调用。如果我不过滤患者,那么按钮只会初始化一次,我对此没有问题。但是如果我执行搜索过滤器,按钮会一次又一次地初始化,所以每当我单击一个按钮时,我都会多次进入另一个 Activity “NewActivity”。我该如何解决这个问题? “populateRecyclerView”将在运行我未在此处提及的查询后调用。

My RecylerAdapter.cs

public class PatientRecyclerAdapter : RecyclerView.Adapter
{
private Context context;
private List<Patient> patientList, filteredPatientList;
public event EventHandler<CheckInClickArgs> CheckInClicked;
MyHolder myholder;

public class CheckInClickArgs : EventArgs
{
public Patient selectedPatient { get; set; }
}

public PatientRecyclerAdapter(Context context, List<Patient> patientList)
{
this.context = context;
this.patientList = patientList;
this.filteredPatientList = patientList;
}

public override int ItemCount
{
get { return filteredPatientList.Count; }
}

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myholder = (MyHolder)holder;
Patient patient = filteredPatientList[position];
myholder.tvPatientName.Text = patient.Fullname;
myholder.btnCheckIn.Click += (sender, e) =>
{
if (CheckInClicked != null)
{
CheckInClicked(this, new CheckInClickArgs { selectedPatient = patient });
}
}

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.layout_patientCardViewRecyler, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}

public void filter(string searchText)
{
searchText = searchText.ToLower();
this.filteredPatientList = patientList.Where(x => x.Fullname.ToLower().Contains(searchText)).ToList();
}
}

class MyHolder : RecyclerView.ViewHolder
{
public TextView tvPatientName;
public Button btnCheckIn;
public MyHolder(View itemView) : base(itemView)
{
tvPatientName = itemView.FindViewById<TextView>(Resource.Id.textViewPatientName);
btnCheckIn = itemView.FindViewById<Button>(Resource.Id.btnCheckIn);
}
}

PatientsTabFragment.cs

public class PatientsTabFragment : Android.Support.V4.App.Fragment, ITextWatcher, TextView.IOnEditorActionListener
{
private Android.Views.View view;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerview_layoutmanager;
private PatientRecyclerAdapter recyclerview_adapter;
private EditText etSearchView;

public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate(Resource.Layout.fragment_patients_tab, container, false);
etSearchView = view.FindViewById<EditText>(Resource.Id.etSearchView);
return view;
}

void populateRecyclerView(List<Patient> patient)
{
recyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerViewPatient);
recyclerview_layoutmanager = new LinearLayoutManager(this.Activity, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(recyclerview_layoutmanager);

recyclerview_adapter = new PatientRecyclerAdapter(this.Activity, patient);
recyclerView.SetAdapter(recyclerview_adapter);
etSearchView.AddTextChangedListener(this);
recyclerview_adapter.CheckInClicked += (sender, e) =>
{
AppController.SelectedPatient = e.selectedPatient;
Activity.StartActivity(typeof(NewActivity));
};
}

public void OnTextChanged(ICharSequence s, int start, int before, int count)
{
recyclerview_adapter.filter(s.ToString());
recyclerview_adapter.NotifyDataSetChanged();
}

最佳答案

在您的 Bind 上,您已将点击设置为这种方式。这是设置只有点击

所以改变你这个代码

myholder.btnCheckIn.Click += (sender, e) =>
{
if (CheckInClicked != null)
{
CheckInClicked(this, new CheckInClickArgs { selectedPatient = patient });
}

对此

if(!myholder.btnCheckIn.HasOnClickListeners)
{
myholder.btnCheckIn.Click += (sender, e) =>
{
if (CheckInClicked != null)
{
CheckInClicked(this, new CheckInClickArgs { selectedPatient = patient });
}
}
}

编辑:

更改您的代码,因为问题出在您的位置。您必须设置 Adapter Position,这将给出 Button Click 的正确位置,请参见下面的代码。

myholder = (MyHolder)holder;
Patient patient = filteredPatientList[myholder.AdapterPosition];
myholder.tvPatientName.Text = patient.Fullname;

关于Android Button Click Event 在实现带有搜索过滤器的 recyclerview 时被多次触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398332/

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