在Recyclerview中隐藏项目时如何填充空白

时间:2018-07-20 14:09:11

标签: android android-recyclerview

我在活动中有一个recyclerview,在每个项目中我都有两个按钮和一个订单号,当负号底部减零时,该订单号为零,我想隐藏该项目,其他项目出现并填充空白。我隐藏该项目使用setVisibility()但我不知道如何处理空白。

这是我的recyclerview适配器:

package com.test.mohammaddvi.snappfood.Adapter;
public class RecyclerViewBuyBasketAdapter extends RecyclerView.Adapter<RecyclerViewBuyBasketAdapter.SingleItemBuyBasket> {

private ArrayList<FinalFood> foodList;
private Context mContext;
private View view;

public RecyclerViewBuyBasketAdapter(ArrayList<FinalFood> foodList, Context mContext) {
    this.foodList = foodList;
    this.mContext = mContext;
}

@NonNull
@Override
public RecyclerViewBuyBasketAdapter.SingleItemBuyBasket onCreateViewHolder(ViewGroup parent, int viewType) {
    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buybasketitem, null);
    return new RecyclerViewBuyBasketAdapter.SingleItemBuyBasket(view);
}

@Override
public void onBindViewHolder(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, int position) {

    FinalFood food = foodList.get(position);
    holder.foodName.setText(food.getName());
    holder.foodDetails.setText(food.getDetails());
    holder.foodPrice.setText(food.getPrice());
    holder.foodOrderNumber.setText(food.getOrdernumber() + "");
    handleClick(holder, view);
}

private void handleClick(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, final View view) {
    holder.foodPlusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FinalFood food = foodList.get(holder.getAdapterPosition());
            int orderNumber = food.getOrdernumber();
            int newOrderNumber = orderNumber + 1;
            food.setOrdernumber(newOrderNumber);
            holder.foodOrderNumber.setText(newOrderNumber + "");
        }
    });
    holder.foodMinusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FinalFood food = foodList.get(holder.getAdapterPosition());
            int orderNumber = food.getOrdernumber();
            if (orderNumber == 1) {
                int newOrderNumber = orderNumber - 1;
                food.setOrdernumber(newOrderNumber);
                view.setVisibility(View.GONE);
            } else {
                int newOrderNumber = orderNumber - 1;
                food.setOrdernumber(newOrderNumber);
                holder.foodOrderNumber.setText(newOrderNumber + "");
            }

        }
    });
}


@Override
public int getItemCount() {
    return (null != foodList ? foodList.size() : 0);
}


public class SingleItemBuyBasket extends RecyclerView.ViewHolder {

    TextView foodName;
    TextView foodPrice;
    Button foodPlusButton;
    Button foodMinusButton;
    TextView foodOrderNumber;
    TextView foodDetails;

    SingleItemBuyBasket(View itemView) {

        super(itemView);
        this.foodName = itemView.findViewById(R.id.foodNameInBuyBasket);
        this.foodPrice = itemView.findViewById(R.id.foodPriceInBuyBasket);
        this.foodDetails = itemView.findViewById(R.id.foodDetailsInBuyBasket);
        this.foodPlusButton = itemView.findViewById(R.id.plusbuttonInBuyBasket);
        this.foodMinusButton = itemView.findViewById(R.id.minusbuttonInBuyBasket);
        this.foodOrderNumber = itemView.findViewById(R.id.ordernumberInBuyBasket);
    }
}
}

这是我的活动:

package com.test.mohammaddvi.snappfood;
public class BuyBasket extends AppCompatActivity{
ArrayList<FinalFood> foods = new ArrayList<>();
RecyclerViewBuyBasketAdapter recyclerViewBuyBasketAdapter;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy_basket);
    recyclerView = findViewById(R.id.recyclerview);
    foods= (ArrayList<FinalFood>) getIntent().getSerializableExtra("final");
    recyclerViewBuyBasketAdapter = new RecyclerViewBuyBasketAdapter(foods, BuyBasket.this);
    recyclerView.setLayoutManager(new LinearLayoutManager(BuyBasket.this, LinearLayoutManager.VERTICAL, false));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(recyclerViewBuyBasketAdapter);
}
}

2 个答案:

答案 0 :(得分:2)

最好删除整个行,而不要更改可见性。这是删除行的代码。您需要调用removeItem(position);按钮onclickListener中的方法

  public void removeItem(int position)
  {
    // Remove specified position
    models.remove(position);
    // Notify adapter to remove the position
    notifyItemRemoved(position);
    // Notify adapter about data changed
    notifyItemChanged(position);
    // Notify adapter about item range changed
    notifyItemRangeChanged(position, arraylist.size());
  }

答案 1 :(得分:0)

在您的xml中,定义两个父级。一个用于隐藏的内容,另一个用于您要在隐藏第一个内容时显示的项目

在单击一个父级或另一个父级时尝试使用View:

layout.setVisibility(View.GONE) or view.setVisibility(View.VISIBLE);