onBindViewHolder中添加监听器
if (mOnItemClickListener != null) {
Log.d(TAG, "onBindViewHolder: 设置Listener");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, position);
}
});
}
activity中使用
chooseAdapter = new HobbyRecycleAdapter(choose, HobbyRecycleAdapter.MODE_ADD);
Log.d(TAG, "initView: "+choose.size());
hobby_recycle_choose.setLayoutManager(new GridLayoutManager(this,3));
chooseAdapter.setmOnItemClickListener(new HobbyRecycleAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.d(TAG, "onItemClick: "+position);
need.add(choose.get(position));
choose.remove(position);
chooseAdapter.notifyDataSetChanged();
needAdapter.notifyDataSetChanged();
}
});
hobby_recycle_choose.setAdapter(chooseAdapter); needAdapter = new HobbyRecycleAdapter(need, HobbyRecycleAdapter.MODE_DELETE);
hobby_recycle_need.setLayoutManager(new GridLayoutManager(this,3));
needAdapter.setmOnItemClickListener(new HobbyRecycleAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.d(TAG, "onItemClick: "+position);
choose.add(need.get(position));
need.remove(position);
chooseAdapter.notifyDataSetChanged();
needAdapter.notifyDataSetChanged();
}
});
hobby_recycle_need.setAdapter(needAdapter);
只有choose的点击事件生效了