У меня есть пользовательский ServerRecyclerAdapter
, в котором я реализовал onLongClickListener
, но теперь у меня возникла проблема, где реализовать OnClickListener
public class ServerRecyclerAdapter extends RecyclerView.Adapter<ServerRecyclerAdapter.ViewHolder> {
//Interface for callbacks
interface ActionCallback {
void onLongClickListener(Server server);
}
private Context context;
private List<Server> serverList;
private ActionCallback mActionCallbacks;
ServerRecyclerAdapter(Context context, List<Server> serverList) {
this.context = context;
this.serverList = serverList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_server, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindData(position);
}
@Override
public int getItemCount() {
return serverList.size();
}
void updateData(List<Server> servers) {
this.serverList = servers;
notifyDataSetChanged();
}
//View Holder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
private TextView mNameTextView;
ViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
mNameTextView = itemView.findViewById(R.id.nameTextView);
}
void bindData(int position) {
Server server = serverList.get(position);
String servername = server.getServerName();
mNameTextView.setText(servername);
}
@Override
public boolean onLongClick(View v) {
if (mActionCallbacks != null) {
mActionCallbacks.onLongClickListener(serverList.get(getAdapterPosition()));
}
return true;
}
}
void addActionCallback(ActionCallback actionCallbacks) {
mActionCallbacks = actionCallbacks;
}