如何让多个按钮进入firestore recyclerview?
Posted
技术标签:
【中文标题】如何让多个按钮进入firestore recyclerview?【英文标题】:How to get multiple buttons working into a firestore recyclerview? 【发布时间】:2021-04-10 02:45:49 【问题描述】:我设法让一个按钮工作 (btnInfo),但后来我不知道如何添加第二个按钮 (btnInschrijven)。关于如何使第二个按钮起作用的任何想法?
我是编码新手,使用 youtube 和逻辑思维,但有些东西我就是找不到或想出。
This is what it looks like
适配器:
public class LesViewHolder extends RecyclerView.ViewHolder
TextView Lijst_Soort, Lijst_Wanneer, Lijst_Waar;
public LesViewHolder(@NonNull View itemView)
super(itemView);
Lijst_Soort = itemView.findViewById(R.id.idLes);
Lijst_Waar = itemView.findViewById(R.id.idWaar);
Lijst_Wanneer = itemView.findViewById(R.id.idWanneer);
btnInfo = itemView.findViewById(R.id.btnInfo);
btnInschrijven = itemView.findViewById(R.id.btnInschrijven);
btnInfo.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
int position = getAdapterPosition();
if(position !=RecyclerView.NO_POSITION && listener !=null)
listener.onItemClick(getSnapshots().getSnapshot(position), position );
);
public LesAdapter getInstance()
return instance;
public interface OnItemClickListener
void onItemClick(DocumentSnapshot documentSnapshot, int position);
public void setOnItemClickListener(OnItemClickListener listener)
this.listener = listener;
MainFragment:
adapter.setOnItemClickListener(new LesAdapter.OnItemClickListener()
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position)
Timestamp wanneer = documentSnapshot.getTimestamp("Wanneer");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E d MMMM HH:mm");
String dateTime = simpleDateFormat.format(wanneer.toDate());
Intent intent = new Intent(getActivity(), Deelnamelijst.class);
intent.putExtra("Welke les", dateTime);
startActivity(intent);
);
【问题讨论】:
【参考方案1】:使用以下代码更改您的代码
public interface OnItemClickListener
void onItemClick(DocumentSnapshot documentSnapshot, int position);
void onSecondButtonClick(DocumentSnapshot documentSnapshot, int position);
//you can add any number of methods here as per your requrenments
现在在第二个按钮上添加点击监听器并执行此操作
btnInschrijven.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
int position = getAdapterPosition();
if(position !=RecyclerView.NO_POSITION && listener !=null)
listener.onSecondButtonClick(getSnapshots().getSnapshot(position), position );
);
现在在您的活动中,无论您在哪里分配 OnItemClickListener
,您都需要覆盖您在 interface
中创建的新方法
因此,当您单击第二个按钮时,onSecondButtonClick()
将执行并且被覆盖方法下的代码将被执行
adapter.setOnItemClickListener(new LesAdapter.OnItemClickListener()
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position)
//...
@Override
public void onSecondButtonClick(DocumentSnapshot documentSnapshot, int position)
//todo here you get click
);
【讨论】:
以上是关于如何让多个按钮进入firestore recyclerview?的主要内容,如果未能解决你的问题,请参考以下文章
Recycler View Android中的Firestore数据[重复]
从 Cloud Firestore 检索时,Recycler View 未加载数据
在 FireStore Ui Recycler Paging Adapter 中使用 Paging 3 时出错:PagingConfig 无法转换为 Config