0

I have a recyclerview which has orders. On clicking order, i need to display the products below the order. So i have used nested recyclerview (vertical-vertical). Sample UI : link

The first order item in recyclerview onclicking show lists the products list below. For the rest of the orders, on cliking order item doesnt display the product list below. The order item expands by ~10 dp with white blank space.

I know nesting recylerview with same orientation is very bad. Is there any other approach shall i use ? or continue with the same nested recyclerview.

Parent Adapter

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) {

    }
}

}

Child Adapter

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()));
    }
}

}

Aswin .A.S
  • 177
  • 1
  • 14

1 Answers1

1

Nested Recyclerview with same orientation is not good for performance. But check out this project on Github. It has done it very neatly. Hope it helps!!

Karan Khurana
  • 575
  • 8
  • 20