我有一个有订单的recyclerview。单击订单时,我需要在订单下方显示产品。所以我使用了嵌套的recyclerview(垂直垂直)。示例 UI:链接
recyclerview 中的第一个订单项目 onclicking show 列出了下面的产品列表。对于其余订单,单击订单项目不会显示下面的产品列表。订单项扩展约 10 dp,带有白色空白区域。
我知道以相同方向嵌套 recylerview 非常糟糕。我还有其他方法吗?或继续使用相同的嵌套回收器视图。
父适配器
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()));
}
}
}