介面回撥實現RecyclerView的條目點選事件
阿新 • • 發佈:2019-01-09
1.介面卡中新增如下程式碼
public interface OnItemClickListener { public void onItemClick(int postion); } OnItemClickListener mClickListener; public void setOnItemClickListener(OnItemClickListener listener) { this.mClickListener = listener; }
@Override public void onBindViewHolder(ViewHolder1 holder, final intposition) { String pic_radio = list.get(position).getPic_radio(); Glide.with(context).load(pic_radio).into(holder.imagerecy); //條目點選事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mClickListener!=null){ mClickListener.onItemClick(position); } } }); holder.textrecy_01.setText(list.get(position).getTitle()); holder.textrecy_02.setText(list.get(position).getAuthor()+" - "+list.get(position).getAlbum_title()); }
Activity
2.Actvivty中呼叫點選事件的介面(介面用介面卡呼叫)
recyAdapter.setOnItemClickListener(newRecyAdapter.OnItemClickListener() { @Override public void onItemClick(int postion) { } });