在重复数据时更改视图的可见性

Change the visibility of a View while repeating data

我通过在我的应用程序中从服务器检索数据来填充 OrderHistory。这里我需要填充依赖于 OrderId 的 listitem。如果 OrderId 重复,我需要隐藏屏幕截图中标记为绿色的 LinearLayout。否则布局必须正常 view.Please 帮助我。

我的适配器Class:

public class MyOrderAdapter extends RecyclerView.Adapter<MyOrderAdapter.ViewHolder> {
    Context mContext;
    List<CartRes> mOrderList = new ArrayList<>();

    private ImageLoader imageLoader;

    public MyOrderAdapter(Context context, List<CartRes> orderList) {
        mContext = context;
        mOrderList = orderList;

        Log.e("Json Adapter", "" + mOrderList);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_order_history, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.cartResOrder = mOrderList.get(position);

        imageLoader = CustomVolleyRequest.getInstance(mContext).getImageLoader();

        String IMAGE_URL = "http://" + Config.IMAGE_URL + holder.cartResOrder.ORDER_IMAGE;
        Log.e("Image URL", IMAGE_URL + "  " + holder.cartResOrder.ORDER_IMAGE);

        imageLoader.get(IMAGE_URL, ImageLoader.getImageListener(holder.orderImage, 0, 0));

        holder.orderImage.setImageUrl(IMAGE_URL, imageLoader);


        holder.txtOrderId.setText(holder.cartResOrder.ORDER_ID);
        holder.txtOrderProduct.setText(holder.cartResOrder.ORDER_PRODUCT);
        holder.txtorderedDate.setText(holder.cartResOrder.ORDER_DATE);
        holder.txtDeliveredDate.setText(holder.cartResOrder.ORDER_DELIVERY_DATE);

    }


    @Override
    public int getItemCount() {
        return mOrderList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtOrderId, txtOrderProduct, txtorderedDate, txtDeliveredDate;
        Button butDetail, butRemove;
        NetworkImageView orderImage;
        CartRes cartResOrder;

        public ViewHolder(View itemView) {
            super(itemView);

            txtOrderId = (TextView) itemView.findViewById(R.id.orderId);
            txtOrderProduct = (TextView) itemView.findViewById(R.id.orderProduct);
            txtorderedDate = (TextView) itemView.findViewById(R.id.orderedDate);
            txtDeliveredDate = (TextView) itemView.findViewById(R.id.orderDeliveredDate);

            butDetail = (Button) itemView.findViewById(R.id.orderViewDetail);
            butRemove = (Button) itemView.findViewById(R.id.orderRemove);

            orderImage = (NetworkImageView) itemView.findViewById(R.id.OrderproductImage);
        }
    }
}

更新:

public void onBindViewHolder(ViewHolder holder, int position) {

    holder.cartResOrder = mOrderList.get(position);

    imageLoader = CustomVolleyRequest.getInstance(mContext).getImageLoader();

    String IMAGE_URL = "http://" + Config.IMAGE_URL + holder.cartResOrder.ORDER_IMAGE;
    Log.e("Image URL", IMAGE_URL + "  " + holder.cartResOrder.ORDER_IMAGE);

    imageLoader.get(IMAGE_URL, ImageLoader.getImageListener(holder.orderImage, 0, 0));

    holder.orderImage.setImageUrl(IMAGE_URL, imageLoader);

    holder.txtOrderId.setText(holder.cartResOrder.ORDER_ID);
    holder.txtOrderProduct.setText(holder.cartResOrder.ORDER_PRODUCT);
    holder.txtorderedDate.setText(holder.cartResOrder.ORDER_DATE);
    holder.txtDeliveredDate.setText(holder.cartResOrder.ORDER_DELIVERY_DATE);

    if (position > 0 && mOrderList.get(position).ORDER_ID == mOrderList.get(position - 1).ORDER_ID) {
        //make sure it is not the first one, and make sure it has the same ID as previous.
        holder.OrderLinear.setVisibility(View.GONE); //hide it, you need to set the reference first.
        holder.txtOrderId.setVisibility(View.GONE);
    }
}

屏幕截图并未说明这一点,但如果您的视图是 RecyclerView 或 ListView,那么我会在将数据提供给适配器之前对数据进行预处理(即将您的模型包装到另一个具有附加属性的 class 中)。

或者,您可以尝试检查前一行(通过从数据源或适配器中获取它),同时填充当前行并调整视图

也许只需将字段 boolean repeatOrderId 添加到您的 CartRes class 并在构造函数中执行一些 for 循环并标记所有项目?你也可以在运行时检查,但是你有相当固定的列表,在构造函数中准备这个标志对性能更好

for(int i=0; i<mOrderList.getCount(); i++){
    CartRes cr = mOrderList.get(i);
    cr.repeatOrderId=true;
    for(int j=0; j<i; j++){
        if(cr.ORDER_ID==mOrderList.get(j).ORDER_ID){
            cr.repeatOrderId=false; //default true
            break;
        }
    }
}

然后在 onBindViewHolder 中检查此标志并 setVisibility(cr.repeatOrderId); 以获得所需的布局

一种方法:

  1. 有一个新的 holder 变量,它指向你想要隐藏的 LinearLayout

  2. 在你的onBindViewHolder()

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //setup everything else above...
    
        if (position > 0 && mOrderList.get(position).ORDER_ID == mOrderList.get(position - 1).ORDER_ID){
            //make sure it is not the first one, and make sure it has the same ID as previous.
            holder.thelinearlayout.setVisibility(View.GONE); //hide it, you need to set the reference first.
        }else holder.thelinearlayout.setVisibility(View.VISIBLE);
            //if somehow order gets changed.(e.g. previous row is deleted).
    
    }
    

编辑:

我假设里面的数据是按ORDER_ID排序的,所以不会出现两个ORDER_ID相同的元素不相邻的情况.

在将列表传递到适配器之前,您可能希望根据 ORDER_ID 对它们进行排序。

编辑2:

添加了评论中建议的其他条件。