gpt4 book ai didi

java - FirebaseRecyclerAdapter 上的 setOnItemClickListener

转载 作者:行者123 更新时间:2023-12-02 11:05:33 26 4
gpt4 key购买 nike

我的项目使用 Firebase Firestore UI 以便在 fragment 中使用 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);
}


};

当适配器本身是一个变量而不是 RecyclerAdapter 的扩展类时,我如何实际设置 onItemClickListener 并传递位置以提取所点击的确切卡片?

谢谢

最佳答案

使用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);
}

请注意,第一个类只是声明您将要单击的 View ,因此,您应该使用自己的变量来代替我提到的变量。

关于java - FirebaseRecyclerAdapter 上的 setOnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002065/

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