我想添加一些子视图(列表项),它们来自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);
}
}
}
答案 0 :(得分:56)
我几天前得到了这个问题,最后解决了这个问题。您所要做的就是 @override布局管理器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 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();
}
}
}
您可以像上面那样复制并粘贴自定义布局管理器,并在父适配器中设置您的recyclerview,如下所示:
RecyclerView.LayoutManager layoutManager = new CustomLinearLayoutManager(mContext);
holder.childRecyclerView.setLayoutManager(layoutManager);
记住:不要使用相同的layoutManager作为父适配器,否则会发生错误。
答案 1 :(得分:14)
如果我理解,您需要使用RecyclerView行的RecyclerView。 在这种情况下,我建议您使用this lib使用可扩展的RecyclerView 。只需点击链接中的expandable example即可。
同一个库中还有很多很酷的功能,比如Drag and Drop,Swipeable行......请注意不到一分钟example video。
您只需将lib添加到gradle.build
文件中的依赖项,如下所示:
dependencies {
compile 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.7.4'
}
能够{java} java文件中的import
。
答案 2 :(得分:14)
支持库版本23.2.0的 Android支持库23.2 。所以WRAP_CONTENT
应该正常工作。
请在 gradle 文件中更新库的版本。
compile 'com.android.support:recyclerview-v7:23.2.0'
这允许RecyclerView
根据其内容的大小自行调整大小。这意味着现在可以使用以前不可用的方案,例如使用WRAP_CONTENT
作为RecyclerView
的维度。您会发现所有内置的LayoutManagers现在都支持自动测量。
您需要致电setAutoMeasureEnabled(true)
以下是示例代码
RecyclerView.LayoutManager layout = new LinearLayoutManager(context);
layout.setAutoMeasureEnabled(true);
由于setAutoMeasureEnabled
已弃用备用解决方案
此方法在API级别27.1.0中已弃用。 LayoutManager
的实现者应该通过覆盖isAutoMeasureEnabled()
来定义它是否使用AutoMeasure。
答案 3 :(得分:0)
请注意:每次调用onBindView()时都不应该创建新的适配器,你应该在onCreateView()中执行一次。
最佳方式 - 例如,使用任何图书馆 - RendererRecyclerViewAdapter
如何添加NestedRecyclerView:
第1步:将ViewModel
界面添加到Modal_Product_List
public class Modal_Product_List implements ViewModel {
String getMedicinename() { ... } //your method
int getQuantity() { ... } //your method
int getQuantitybasedprice() { ... } //your method
}
第2步:为ViewBinder
创建Modal_Product_List
:
private ViewRenderer getModalProductViewRenderer() {
return new ViewBinder<>(
R.layout.item_row_medicine_productlist, //your child layout id
Modal_Product_List.class //your child item class
(model, finder, payloads) -> finder
.setText(R.id.medicineName, model.getMedicinename())
.setText(R.id.medQty, (String) model.getQuantity())
.setText(R.id.amount, (String) model.getQuantitybasedprice())
.setChecked(R.id.selectMe, ...)
);
}
第3步:将CompositeViewModel
界面添加到PrescriptionModal
或从DefaultCompositeModel
延伸:
public class PrescriptionModal extends DefaultCompositeViewModel {
String getPrescriptionID() {...} //your method
String getDoctorType() {...} //your method
String getDoctorName() {...} //your method
String getPatientName() {...} //your method
@Override
List<Modal_Product_List> getItems() { return yourProductItems; }
}
第4步:为ViewBinder
创建PrescriptionModal
:
private ViewRenderer getModalProductViewRenderer() {
return new CompositeViewBinder<>(
R.layout.item_row_digitised_request, //your parent item layout
R.id.lstAllMedicines, //your nested RecyclerView
PrescriptionModal.class, //your parent item class
(model, finder, payloads) -> finder
//no need to set child items, it will set automatically
.setText(R.id.prescnum, "Prescription:" + model.getPrescriptionID)
.setText(R.id.doctorName, "Doctor:" + model.getDoctorName())
.setText(R.id.doctorType, "Type:" + model.getDoctorType())
.setText(R.id.patientName, "Patient:" + model.getPatientName())
).registerRenderer(getModalProductViewRenderer()); //register ModalProductViewRenderer
.registerRenderer(...) //if you need you can create other renderer and register here
);
步骤5(可选):如果您需要自定义LayoutManager
,请展开CompositeViewBinder
并覆盖createLayoutManager
方法,然后使用它而不是{ {1}}
CompositeViewBinder
第6步:初始化public class CustomCompositeViewBinder extends CompositeViewBinder {
//...
@Override
protected RecyclerView.LayoutManager createLayoutManager() {
return new MyLinearLayoutManager(getContext(), VERTICAL, false);
}
}
并注册渲染器:
RendererRecyclerViewAdapter
这是添加嵌套RecyclerView
的简单方法