我有一个带有订单的recyclerview。 点击订单后,我需要在订单下方显示产品。所以我用过嵌套的recyclerview(vertical-vertical)。 界面示例:link
recyclerview onclicking show中的第一订单项列出了下面的产品列表。对于其余的订单,按取消订单项目不会在下面显示产品列表。订单商品会以大约10 dp的速度扩展,并带有白色空白。
我知道以相同的方向嵌套recylerview是非常糟糕的。我还可以使用其他方法吗?或继续使用同一嵌套的recyclerview。
父适配器
public class MyOrdersAdapter extends RecyclerView.Adapter<MyOrdersAdapter.MyViewHolder> {
private static final String TAG = MyOrdersAdapter.class.getSimpleName();
private List<RecentOrder> recentOrders;
private Context context;
private OrderHistoryInteractor orderHistoryInteractor;
public MyOrdersAdapter(@NonNull Context context, List<RecentOrder> recentOrders) {
this.recentOrders = recentOrders;
this.context = context;
if (context instanceof OrderHistoryInteractor)
orderHistoryInteractor = (OrderHistoryInteractor) context;
else
throw new ClassCastException("MyOrdersAdapter must implement OrderHistoryInteractor");
}
@Override
public MyOrdersAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_orders_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
RecentOrder recentOrder = recentOrders.get(position);
try {
//Glide.with(context).load(recentOrder.getProductDetails().getProductThumbImageUrl()).into(holder.orderImage);
} catch (NullPointerException e) {
e.printStackTrace();
}
if (holder.productAdapter == null) {
holder.orderedProductsList.setHasFixedSize(true);
holder.productAdapter = new MyOrdersProductAdapter(context, recentOrders.get(holder.getAdapterPosition()).getOrderItems());
holder.orderedProductsList.setAdapter(holder.productAdapter);
holder.orderedProductsList.setLayoutManager(new LinearLayoutManager(context));
holder.orderedProductsList.addItemDecoration(new VerticalSpaceItemDecoration(AndroidUtils.dpToPx(context, 8)));
}
holder.orderedProductsList.setVisibility(View.VISIBLE);
holder.productName.setText(recentOrder.getOrderId());
holder.orderQuantity.setText(recentOrder.getOrderId());
holder.rootView.setOnClickListener(v -> {
Log.d(TAG, "OnClickListener Works for toggling visibility!!");
if ((holder.orderedProductsList.getVisibility() == View.VISIBLE))
holder.orderedProductsList.setVisibility(View.GONE);
else
holder.orderedProductsList.setVisibility(View.VISIBLE);
});
//holder.orderAmount.setText(String.valueOf(recentOrder.getItemPrice()));
}
@Override
public int getItemCount() {
return recentOrders.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public RecyclerView orderedProductsList;
public MyOrdersProductAdapter productAdapter;
public ImageView orderImage;
public TextView productName, orderQuantity, orderAmount;
public View rootView;
public MyViewHolder(View view) {
super(view);
rootView = view;
productName = view.findViewById(R.id.productName);
orderImage = view.findViewById(R.id.orderImage);
orderAmount = view.findViewById(R.id.orderAmount);
orderQuantity = view.findViewById(R.id.orderQuantity);
orderedProductsList = view.findViewById(R.id.orderedProductsList);
view.setOnClickListener(this::onClick);
}
@Override
public void onClick(View v) {
}
}
}
儿童适配器
public class MyOrdersProductAdapter extends RecyclerView.Adapter<MyOrdersProductAdapter.MyViewHolder> {
private List<RecentProduct> recentProducts;
private Context context;
private OrderHistoryInteractor orderHistoryInteractor;
public MyOrdersProductAdapter(@NonNull Context context, List<RecentProduct> recentProducts) {
this.recentProducts = recentProducts;
this.context = context;
if (context instanceof OrderHistoryInteractor)
orderHistoryInteractor = (OrderHistoryInteractor) context;
else
throw new ClassCastException("MyOrdersProductAdapter must implement OrderHistoryInteractor");
}
@Override
public MyOrdersProductAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_orders_products_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
RecentProduct recentProduct = recentProducts.get(position);
try {
Glide.with(context).load(recentProduct.getProduct().getProductThumbImageUrl()).into(holder.orderImage);
} catch (NullPointerException e) {
e.printStackTrace();
}
holder.productName.setText(recentProduct.getProduct().getProductName());
holder.orderQuantity.setText(String.valueOf(recentProduct.getItemOrderQty()));
holder.orderAmount.setText(String.valueOf(recentProduct.getItemPrice()));
}
@Override
public int getItemCount() {
return recentProducts.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView orderImage;
public TextView productName, orderQuantity, orderAmount;
public MyViewHolder(View view) {
super(view);
productName = view.findViewById(R.id.productName);
orderImage = view.findViewById(R.id.orderImage);
orderAmount = view.findViewById(R.id.orderAmount);
orderQuantity = view.findViewById(R.id.orderQuantity);
view.setOnClickListener(this::onClick);
}
@Override
public void onClick(View v) {
//orderHistoryInteractor.onRecentOrderSelected(recentProducts.get(getAdapterPosition()));
}
}
}