我在自定义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;
}
答案 0 :(得分:0)
尝试一下
ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (mActionCallbacks != null) {
mActionCallbacks.onLongClickListener(serverList.get(getAdapterPosition()));
}
return true;
}
});
}
答案 1 :(得分:0)
怎么样...
extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener
然后它将建议您在哪里插入该接口方法的替代。
答案 2 :(得分:0)
对于其他人如何解决这个简单的问题:)
我已经添加了ActionCallback
void onClickListener(Server server);
和ViewHolder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private TextView mNameTextView;
ViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
mNameTextView = itemView.findViewById(R.id.nameTextView);
}
.
.
.
@Override
public void onClick(View view) {
if (mActionCallbacks != null) {
mActionCallbacks.onClickListener(serverList.get(getAdapterPosition()));
}
}
感谢@MikeM。帮助我解决了