我正在构建购物车方法,我从响应中获取对象列表,将其添加到适配器(Recyclerview.Adapter)。每个对象都有它的数量,在视图中,我有一个加号按钮和一个减号按钮,当点击时我改变了对象的数量。 当我完成点击加号和减号按钮后,(在保留一定数量的物品后),按一下检查购物车按钮。当我按下该按钮时,我需要在适配器上有一个修改版本的列表的子列表,仅包含具有正数量的对象。 到目前为止这是我的代码: 我的ViewHolder上的按钮:
@OnClick(R.id.item_items_order_now_plus)
public void onAddItemClicked() {
ShoppingCartList.getInstance().increaseTotalSize();
dataItem.increaseQuantity();
shoppingCartListChanged.smthChanged();
plusclicked++;
previouslyRemoved = true;
ShoppingCartList.getInstance().getShoppingCartItems().add(getAdapterPosition(), dataItem);
if (plusIncreaseNumber.getVisibility() == View.GONE) {
plusIncreaseNumber.setVisibility(View.VISIBLE);
}
plusIncreaseNumber.setText(plusclicked + "");
}
@OnClick(R.id.item_items_order_now_minus)
public void onRemoveItemClicked() {
if (plusIncreaseNumber.getVisibility() == View.GONE) {
return;
}
if (plusclicked > 0) {
dataItem.increaseQuantity();
ShoppingCartList.getInstance().decreaseTotalSize();
plusclicked--;
plusIncreaseNumber.setText(plusclicked + "");
}
if (plusclicked == 0) {
if (previouslyRemoved) {
ShoppingCartList.getInstance().getShoppingCartItems().remove(getAdapterPosition());
}
plusIncreaseNumber.setVisibility(View.GONE);
return;
}
}
ShoppingCartListClass: (用于保存要显示它们的项目总数,并且只使用一个列表)
public class ShoppingCartList {
private static ShoppingCartList shoppingCartList;
private static int totalSize;
private ShoppingCartList(){
}
public static ShoppingCartList getInstance(){
if (shoppingCartList==null){
shoppingCartList=new ShoppingCartList();
}
return shoppingCartList;
}
private static List<al.baboon.restaurant.restsapp.response.dashboardReport.specificCategoriesResponse.DataItem> shoppingCartItems=new ArrayList<>();
public List<al.baboon.restaurant.restsapp.response.dashboardReport.specificCategoriesResponse.DataItem> getShoppingCartItems(){
return shoppingCartItems;
}
public static int getTotalSize(){
return totalSize;
}
public static void increaseTotalSize(){
totalSize++;
}
public static void decreaseTotalSize(){
if (totalSize>0){
totalSize--;
}
}
public static void emptyTotalSize(){
totalSize=0;
}
}