本文主要介绍:如何给RecyclerView添加点击事件。在这之前,小编搜索过,但没有搜到步骤很全的文章。所以,以此记录,希望能对一些朋友有所帮助。
给RecyclerView添加点击事件,主要分为三步:
第一步:定义事件接口
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
第二步:设置RecyclerView数据适配器
以下只列出了部分重要代码,文末有整个Acticity代码:
public class MyAdapter extends RecyclerView.Adapter implementsView.OnClickListener {
private OnItemClickListener mOnItemClickListener = null;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_main, null);
// 给View注册点击事件
view.setOnClickListener((View.OnClickListener) this);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder holder2 = (MyViewHolder) holder;
Model model = mData.get(position);
holder2.userName.setText(model.getName());
// 将position保存在itemView的Tag中,以便点击时进行获取
holder2.itemView.setTag(position);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
// 注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v, (int)v.getTag());
}
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
第三步:监听点击事件
myAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position){
// 在这里处理item的点击事件
}
});
Activity完整代码:
public class MainActivity extends AppCompatActivity {
private MyAdapter myAdapter;
private ArrayList<Model> list = new ArrayList<>();// 数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myAdapter = newWaterFallAdapter(this, list);
// 找到RecyclerView控件
RecyclerView mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mRecyclerView.setAdapter(myAdapter);
// 设置监听事件
myAdapter.setOnItemClickListener(newOnItemClickListener() {
@Override
public voidonItemClick(View view, int position){
// 在这里处理item的点击事件,position就是点击的item
}
});
}
// 定义接口
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public class MyAdapter extends RecyclerView.Adapter implements View.OnClickListener {
private final Context mContext;
private ArrayList<Model> mData;//定义数据源
private OnItemClickListener mOnItemClickListener = null;
//定义适配器构造方法,默认传入上下文和数据源
public MyAdapter(Context context, ArrayList<Model> data) {
mContext = context;
mData = data;
}
// 将ItemView渲染进来,创建ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_main, null);
//将创建的View注册点击事件
view.setOnClickListener((View.OnClickListener) this);
return new MyViewHolder(view);
}
// 将数据源的数据绑定到相应控件上
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, intposition) {
MyViewHolder holder2 = (MyViewHolder) holder;
HomeModel model = mData.get(position);
holder2.userName.setText(model.getName());
// 将position保存在itemView的Tag中,以便点击时进行获取
holder2.itemView.setTag(position);
}
@Override
public int getItemCount() {
return list.size();
}
//定义自己的ViewHolder,将View的控件引用在成员变量上
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView userName;
public MyViewHolder(View itemView) {
super(itemView);
userName = (TextView) itemView.findViewById(R.id.recycler_view_tv);
}
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v, (int)v.getTag());
}
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}