所有的代码都已经写好,OnClick写在ViewHodler里面,在activity中调用setItemClickListener,但是运行时item无法点击且并无bug爆出。谢谢!
PS:OncreatViewHolder之前忘记贴出来了,然而还是不行。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main,parent,false);
MainViewHolder holder = new MainViewHolder(v,mItemClickListener);
return holder;
}
public interface ItemClickListener {
void OnItemClick(View view,int position);
}
public void setItemClickListener(ItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public class MainViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mTextView;
private ImageView mImageView;
private ItemClickListener mItemClickListener;
public MainViewHolder(View itemview,ItemClickListener itemClickListener) {
super(itemview);
mTextView = (TextView) itemview.findViewById(R.id.main_text_view);
mImageView = (ImageView) itemview.findViewById(R.id.main_pic);
itemview.setOnClickListener(this);
this.mItemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
Log.d("ss", "ff");
mItemClickListener.OnItemClick(itemView,getAdapterPosition() );
}
}
}
}
Activity
mAdapter.setItemClickListener(new MainAdapter.ItemClickListener() {
@Override
public void OnItemClick(View view, int position) {
switch (position){
case 1:
Log.d("ss","ff");
Intent intent = new Intent(MainActivity.this,NewsActivity.class);
startActivity(intent);
}
}
});