在Adapter适配器中设置
public void onBindViewHolder(Adapter.MyHolder holder, int position) { holder.img.setImageResource(list.get(position).getImg()); holder.tv1.setText(list.get(position).getName()); holder.tv2.setText(list.get(position).getText()); holder.linear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
}
});
holder.linear.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return false;
}
});
}
如果需要传值到Activity中 直接在点击事件中Intent方法intent.put就好
如果要传到Fragment则需要创建接口
public MyHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.img); tv1 = itemView.findViewById(R.id.tv1); tv2 = itemView.findViewById(R.id.tv2); linear = itemView.findViewById(R.id.linear); linear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(context,Main3Activity.class); context.startActivity(intent); } }); linear.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { onand.shouText(getLayoutPosition()); return false; } }); } } //定义一个接口 public interface Onand { public void shouText(Integer poss); } //声明接口变量 public Onand onand; //提供方法,暴露接口 public void setOnand(Onand onand) { this.onand = onand; }