Vertical RecyclerView 内的 Vertical RecyclerView [英] Vertical RecyclerView inside Vertical RecyclerView

查看:32
本文介绍了Vertical RecyclerView 内的 Vertical RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有订单的回收站.单击订单时,我需要在订单下方显示产品.所以我使用了嵌套的 recyclerview (vertical-vertical).示例用户界面:link

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

recyclerview 中的第一个订单项点击显示列出了下面的产品列表.对于其余订单,点击订单项目不会显示下面的产品列表.订单项扩大约 10 dp 并留有空白.

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.

我知道嵌套具有相同方向的 recylerview 非常糟糕.我还有其他方法可以使用吗?或继续使用相同的嵌套回收器视图.

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.

父适配器

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

}

推荐答案

具有相同方向的嵌套 Recyclerview 对性能不利.但是在 Github 上查看这个项目.它做得非常巧妙.希望能帮到你!!

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!!

这篇关于Vertical RecyclerView 内的 Vertical RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆