指定的子项已经有一个父项,同时将第二个视图添加到内部recyclerview内的布局

时间:2018-06-15 20:01:41

标签: android android-recyclerview android-linearlayout recycler-adapter viewgroup

我知道有很多问题与此有关,但我仍然不清楚错误的根本原因。

所以,我有一个CardViews的RecyclerView,并在每个我试图拥有另一个RecyclerView。这个内部RecyclerView位于带有TextView的LinearLayout中,TextView仅在扩展时显示。

由于我将这个TextView和布局与RecyclerView以编程方式添加到CardView中的布局,因此内部RecyclerView处于另一种布局中。我遇到的麻烦是以编程方式将此布局添加到CardView布局中。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.price_list_item, parent, false);
    View child = LayoutInflater.from(context).inflate(R.layout.price_details_recyclerview, parent, false);
    return new PriceListAdapter.ViewHolder(view, child);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    if (detailsList== null || detailsList.isEmpty())
        Log.d("Map", "EmptyyyyyyyyyyAdapter");
    else
        Log.d("Adapter List size:", "" + detailsList.size() + " " + detailsList.get(position).getBrandName());
    holder.brandName.setText(detailsList.get(position).getBrandName()); // set heading
    holder.subBrandName.setText("");
    setTextSubBrand = new StringBuilder();
    priceDetailsStringVal = new StringBuilder();
    holder.detailsLayout;.removeAllViews(); // else textviews get added each time expand is pressed

    linearLayoutManager = new LinearLayoutManager(context);
    priceDetailsAdapter = new PriceDetailsAdapter(context, detailsList.get(position).getpriceList());
    //  priceDetailsRecycler = new RecyclerView(context);
    holder.priceDetailsRecycler.setHasFixedSize(true);
    holder.priceDetailsRecycler.setLayoutManager(linearLayoutManager);
    holder.priceDetailsRecycler.setAdapter(priceDetailsAdapter);

    for (Price lp : detailsList.get(position).getriceList()) {

        Field[] fields = lp.getClass().getDeclaredFields(); //get key and value for price details
        setTextSubBrand.append(lp.documentId);
        setTextSubBrand.append(" | ");
        TextView subBrandText = new TextView(context);
        subBrandText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        subBrandText.setText(lp.documentId);
        holder.detailsLayout;.addView(subBrandText);


        holder.detailsLayout;.addView(holder.viewForRecycler);   //GETS ERROR HERE

    }


// other functionalities

}


public class ViewHolder extends RecyclerView.ViewHolder {

    View view;
    View viewForRecycler;

    public Button expandButton;
    public TextView brandName;
    public TextView subBrandName;
    public LinearLayout detailsLayout;;
    public RecyclerView priceDetailsRecycler;

    public ViewHolder(View itemView, View child) {
        super(itemView);
        view = itemView;
        viewForRecycler = child;
        brandName = view.findViewById(R.id.brandName);
        subBrandName = view.findViewById(R.id.subBrands);
        detailsLayout; = view.findViewById(R.id.detailsLayout;);

        priceDetailsRecycler = viewForRecycler.findViewById(R.id.price_details_reyclerview);
    }
}

任何有助于我了解原因和解决方案的帮助将不胜感激。 谢谢

0 个答案:

没有答案