0

我有一個片段中項目的根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); 

}

+0

發佈您的代碼太 –

+0

@AdnanAmjad這裏是我的代碼。 –

回答

0

你可以在這裏嘗試這種

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{ 

    public static class ViewHolder extends RecyclerView.ViewHolder { 

     //your other views 
     private View parentView; 

     public ViewHolder(View v) { 
      super(v); 
      parentView = v; 
     } 

     public View getParentView(){ 
      return parentView; 
     } 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder vh, final int position) { 
     View.OnClickListener anOnClickListener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       aMethodThatAddNewRecyclerViewDynamicallyToThisRecyclerView(); 
      } 
     }; 
     vh.getParentView().setOnClickListener(anOnClickListener); 
    } 
} 
+0

Thankyou,但是當我使用這種方法時,點擊僅響應recyclerview的第一項而不響應其他recyclerviews。還有其他的方法嗎? –

相關問題