我的项目使用Firebase Firestore UI
以便在片段中使用RecyclerView
+ CardView
组合显示集合,我使用了FirestoreUI
本身提供的适配器。
FirestoreRecyclerAdapter
但是,我无法在显示的每张卡上设置
onItemClickListener
。FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Record, recordViewHolder>(options) {
@Override
public void onDataChanged() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError(@NonNull FirebaseFirestoreException e) {
Log.e(TAG, "onError: ", e);
}
@Override
protected void onBindViewHolder(@NonNull recordViewHolder holder, int position, @NonNull Record record) {
holder.date.setText(String.valueOf(record.getFullDate()));
holder.qty.setText(String.valueOf(record.getQty()));
holder.rate.setText(String.valueOf(record.getRate()));
holder.total.setText(String.valueOf(record.getTotal()));
holder.milkType.setText(String.valueOf(record.getMilkType()));
}
@NonNull
@Override
public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.record_logs_card, parent, false);
return new recordViewHolder(view, mListener);
}
};
像这样设置
onItemClickListener
会导致语法错误。FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Record, recordViewHolder>(options, setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});) {
@Override
public void onDataChanged() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError(@NonNull FirebaseFirestoreException e) {
Log.e(TAG, "onError: ", e);
}
@Override
protected void onBindViewHolder(@NonNull recordViewHolder holder, int position, @NonNull Record record) {
holder.date.setText(String.valueOf(record.getFullDate()));
holder.qty.setText(String.valueOf(record.getQty()));
holder.rate.setText(String.valueOf(record.getRate()));
holder.total.setText(String.valueOf(record.getTotal()));
holder.milkType.setText(String.valueOf(record.getMilkType()));
}
@NonNull
@Override
public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.record_logs_card, parent, false);
return new recordViewHolder(view, mListener);
}
};
当适配器本身是变量而不是
onItemClickListener
的扩展类时,如何设置RecyclerAdapter
并传递位置以提取准确的卡?谢谢
最佳答案
制作一个界面以使用RecyclerViews
处理点击
为此,首先创建一个名为FirebaseViewHolder
的类
public class FirebaseViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mPostTitle;
public TextView mPostDesc;
public FirebaseViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.image_recyclerview);
mPostTitle = (TextView) itemView.findViewById(R.id.tv_title_recyclerview_item);
mPostDesc = (TextView) itemView.findViewById(R.id.tv_desc_recyclerview_item);
//listener set on ENTIRE ROW, you may set on individual components within a row.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mClickListener.onItemLongClick(v, getAdapterPosition());
return true;
}
});
}
private FirebaseViewHolder.ClickListener mClickListener;
//Interface to send callbacks...
public interface ClickListener{
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
public void setOnClickListener(FirebaseViewHolder.ClickListener clickListener){
mClickListener = clickListener;
}
}
然后在您的
onCreateViewHolder
@NonNull
@Override
public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.record_logs_card, parent, false);
viewHolder.setOnClickListener(new FirebaseViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getActivity(), "Item clicked at " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(getActivity(), "Item long clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return new recordViewHolder(view, mListener);
}
请注意,第一个类仅声明要单击的视图,因此,您应该使用自己的视图来代替我提到的变量。
关于java - FirebaseRecyclerAdapter上的setOnItemClickListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51002065/