我正在尝试,但是当我同时实现这两个时,ViewHolder无法达到这些。可能吗?我的代码:
public final class SomeViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
// doesn't run
}
@Override
public boolean onLongClick(View view) {
// doesn't run
}
}
如果我删除其中一个监听器,如下所示,它可以正常工作。
public final class SomeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// run properly
}
}
答案 0 :(得分:-1)
创建一个绑定方法或任何你想要调用它的方法。
public void bind(ModelClass model, MyInterface listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemClicked(view);
}
});
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemLongClicked(view);
}
});
}
在
上onBindViewHolder()
holder.bind(item.get(position), listener);
宣布你的
时Adapter
adapter = new MyAdapter(item, new OnItemClick() {
@Override
public void itemClicked(View view, ModelClass model) {
//event
}
@Override
public void itemLongClicked(View view, ModelClass model) {
return true;
}
});