如何在一个父RecyclerView里面有一个ListView / RecyclerView?

我想添加一些子视图(列表项),从JSON格式的数据来我。 每个子列表都在父列表项行的下面。 如何在RecyclerView为每个行项目填充它(带有子列表项目的父项目)?

我已经尝试在RecyclerView父行(用于填充子列表)中使用RecyclerView ,但在这里子视图不可见。

父适配器类

 public class DigitizedPrescAdapter extends RecyclerView.Adapter<DigitizedPrescAdapter.ListItemViewHolder>{ private List<PrescriptionModal> prescriptionList; MedicinesInPrescAdapter adapter; public DigitizedPrescAdapter(List<PrescriptionModal> prescriptionListModal) { if (prescriptionListModal == null) { throw new IllegalArgumentException( "PrescriptionList must not be null"); } this.prescriptionList = prescriptionListModal; } @Override public ListItemViewHolder onCreateViewHolder( ViewGroup viewGroup, int viewType) { View itemView = LayoutInflater. from(viewGroup.getContext()). inflate(R.layout.item_row_digitised_request, viewGroup, false); return new ListItemViewHolder(itemView); } @Override public void onBindViewHolder( ListItemViewHolder viewHolder, int position) { PrescriptionModal model = prescriptionList.get(position); viewHolder.prescnum.setText("Prescription "+ ++position); viewHolder.prescNo.setText("Prescription: "+model.getPrescriptionID()); viewHolder.doctorType.setText("Type: "+model.getDoctorType()); viewHolder.doctorName.setText("Doctor: "+model.getDoctorName()); viewHolder.patientName.setText("Patient: "+model.getPatientName()); adapter = new MedicinesInPrescAdapter(model.getLstproduct()); viewHolder.lstMedicines.setAdapter(adapter); } @Override public int getItemCount() { return prescriptionList.size(); } public final static class ListItemViewHolder extends RecyclerView.ViewHolder { TextView prescnum; TextView prescNo; TextView doctorType; TextView patientName; TextView doctorName; CheckBox selectAll; RecyclerView lstMedicines; public ListItemViewHolder(View itemView) { super(itemView); prescnum = (TextView) itemView.findViewById(R.id.prescnum); prescNo = (TextView) itemView.findViewById(R.id.prescNo); doctorType = (TextView) itemView.findViewById(R.id.doctorType); patientName = (TextView) itemView.findViewById(R.id.patientName); doctorName = (TextView) itemView.findViewById(R.id.doctorName); selectAll = (CheckBox) itemView.findViewById(R.id.selectAll); lstMedicines = (RecyclerView) itemView.findViewById(R.id.lstAllMedicines); MyLinearLayoutManager layoutManager = new MyLinearLayoutManager(itemView.getContext(),LinearLayoutManager.VERTICAL,false); lstMedicines.setHasFixedSize(false); lstMedicines.setLayoutManager(layoutManager); } } } 

子适配器类

 public class MedicinesInPrescAdapter extends RecyclerView.Adapter<MedicinesInPrescAdapter.MedicineListItemViewHolder>{ List<Modal_Product_List> prescriptionProducts; public MedicinesInPrescAdapter(List<Modal_Product_List> prescriptionListProd) { if (prescriptionListProd == null) { throw new IllegalArgumentException( "PrescriptionProductList must not be null"); } this.prescriptionProducts = prescriptionListProd; } @Override public MedicineListItemViewHolder onCreateViewHolder( ViewGroup viewGroup, int viewType) { View itemView = LayoutInflater. from(viewGroup.getContext()). inflate(R.layout.item_row_medicine_productlist, viewGroup, false); return new MedicineListItemViewHolder(itemView); } @Override public void onBindViewHolder( MedicineListItemViewHolder viewHolder, int position) { Modal_Product_List modelMedicine = prescriptionProducts.get(position); viewHolder.medicineName.setText(modelMedicine.getMedicinename()); viewHolder.medQty.setText(modelMedicine.getQuantity()); viewHolder.days.setText("30"); viewHolder.Amount.setText(modelMedicine.getQuantitybasedprice()); } @Override public int getItemCount() { return prescriptionProducts.size(); } public final static class MedicineListItemViewHolder extends RecyclerView.ViewHolder { TextView medicineName; EditText medQty; TextView days; TextView Amount; CheckBox selectMe; public MedicineListItemViewHolder(View itemView) { super(itemView); medicineName = (TextView) itemView.findViewById(R.id.medicineName); medQty = (EditText) itemView.findViewById(R.id.medQty); days = (TextView) itemView.findViewById(R.id.days); Amount = (TextView) itemView.findViewById(R.id.amount); selectMe = (CheckBox) itemView.findViewById(R.id.selectMe); } } } 

前几天我得到了这个问题,并最终解决了这个问题。 所有你需要做的是@覆盖布局pipe理器onMeasure函数如下:

CustomLinearLayoutManager

 public class CustomLinearLayoutManager extends LinearLayoutManager { private static final String TAG = CustomLinearLayoutManager.class.getSimpleName(); public CustomLinearLayoutManager(Context context) { super(context); } public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } private int[] mMeasuredDimension = new int[2]; @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { final int widthMode = View.MeasureSpec.getMode(widthSpec); final int heightMode = View.MeasureSpec.getMode(heightSpec); final int widthSize = View.MeasureSpec.getSize(widthSpec); final int heightSize = View.MeasureSpec.getSize(heightSpec); int width = 0; int height = 0; for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), mMeasuredDimension); if (getOrientation() == HORIZONTAL) { width = width + mMeasuredDimension[0]; if (i == 0) { height = mMeasuredDimension[1]; } } else { height = height + mMeasuredDimension[1]; if (i == 0) { width = mMeasuredDimension[0]; } } } switch (widthMode) { case View.MeasureSpec.EXACTLY: width = widthSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: } switch (heightMode) { case View.MeasureSpec.EXACTLY: height = heightSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: } setMeasuredDimension(width, height); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { try { View view = recycler.getViewForPosition(0);//fix dynamic添加时报IndexOutOfBoundsException if (view != null) { RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width); int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec); measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; recycler.recycleView(view); } } catch (Exception e) { e.printStackTrace(); } } } 

你可以像上面那样复制和通过自定义布局pipe理器,并在父适配器中设置你的recyclerview像这样:

 RecyclerView.LayoutManager layoutManager = new CustomLinearLayoutManager(mContext); holder.childRecyclerView.setLayoutManager(layoutManager); 

记住 :不要使用与父适配器相同的layoutManager,否则会发生错误。

通过支持库版本23.2.0的Android支持库23.2 。 所以所有的WRAP_CONTENT都应该正常工作。

请在gradle文件中更新库的版本。

 compile 'com.android.support:recyclerview-v7:23.2.0' 

这允许RecyclerView根据其内容的大小自行resize。 这意味着以前不可用的scheme,例如对RecyclerView的维度使用WRAP_CONTENT ,现在是可能的。 您会发现所有内置的LayoutManagers现在都支持自动测量。

你将被要求调用setAutoMeasureEnabled(true)

以下是示例代码

 RecyclerView.LayoutManager layout = new LinearLayoutManager(context); layout.setAutoMeasureEnabled(true); 

如果我明白,你想要RecyclerView与RecyclerView行。 在这种情况下,我build议你使用可扩展的RecyclerView ,使用这个库 。 只需按照链接中的可扩展示例 。

在同一个库中还有很多很酷的function,比如拖放,可以滑动的行……观看这个不到一分钟的示例video 。

您只需将lib添加到您的gradle.build文件中的依赖关系中,如下所示:

 dependencies { compile 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.7.4' } 

为了能够在你的java文件中import lib。