RecyclerView 點擊事件和長按事件

在Adapter適配器中設置ide

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就好this

若是要傳到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;
}
相關文章
相關標籤/搜索