我有一個片段中項目的根recyclerview。點擊回收站視圖中的每個項目,它會使用與根回收站視圖相同的適配器生成動態回收站視圖列表。我想將OnClick偵聽器實現到child recyclerview,並使用第二級recyclerview生成下一級項目。我已經尋找了很多答案,但是我發現它是爲根項目實現的點擊監聽器。我在我的適配器中實現了我的偵聽器在視圖中。如何將OnClickListener實現爲內部子動態創建的RecyclerView?
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{
private Context context;
private List<ItemgroupDb> itemgroupDbs;
private ItemgroupDb PItemID;
private RecyclerViewClickListener itemListener;
public ItemAdapter(Context context, List<ItemgroupDb> itemgroupDbs, RecyclerViewClickListener itemListener) {
this.context = context;
this.itemgroupDbs = itemgroupDbs;
this.itemListener = itemListener;
}
public ItemAdapter(Context context, List<ItemgroupDb> itemgroupDbs) {
this.context = context;
this.itemgroupDbs = itemgroupDbs;
}
public ItemAdapter(Context context, List<ItemgroupDb> itemgroupDbs, ItemgroupDb PItemID, RecyclerViewClickListener itemListener) {
this.context = context;
this.itemgroupDbs = itemgroupDbs;
this.PItemID = PItemID;
this.itemListener = itemListener;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return itemgroupDbs.size();
}
@Override
public void onBindViewHolder(ItemAdapter.ItemViewHolder itemViewHolder, int i) {
ItemgroupDb itemgroupDb = itemgroupDbs.get(i);
itemViewHolder.vName.setText(itemgroupDb.getItemName());
Log.e("Itemname", String.valueOf(itemgroupDb.getItemName()));
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.child3_row, viewGroup, false);
return new ItemViewHolder(itemView);
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView vName;
protected ImageView vImage;
public ItemViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.childname);
vImage=(ImageView)v.findViewById(R.id.iv_item_image);
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemListener.recyclerViewListClicked(v, 1, this.getLayoutPosition(), itemgroupDbs.get(this.getLayoutPosition()).getLevel());
notifyDataSetChanged();
}
}
接口clicklistener
public interface RecyclerViewClickListener {
public void recyclerViewListClicked(View v, int id, int level, int position);
}
發佈您的代碼太 –
@AdnanAmjad這裏是我的代碼。 –