使用notifyItemRemoved()

时间:2018-06-17 13:12:55

标签: android recycler-adapter

我正在开发一个应用程序,该应用程序在包含菜单项的片段中包含recyclerviewA,在另一个recyclerviewB中选择另一个recyclerviewB作为购物车。当在recyclerA中选择一个项目时,可以使用在recyclerviewB中更新的相同项目的每个项目行上的2个按钮(用于添加和删除项目)来增加或减少其编号。如果项目编号减少到零0,则该项目将从recyclerviewB中删除。当在recyclerviewA中为每个项目使用加或减按钮时,这很有效。在减少recyclerviewB中所选项目的数量时会出现问题。在从上到下移除第一个项目后,项目位置不会更新。例如,当项目0被移除时,项目1保留在项目1而不是将其更新到项目0所以adapterPosition为0,因此返回该位置的空项。从下往上删除项目时我没有这个问题。

适用于RecyclerviewA的Adadpater

public class MenuListAdapter extends RecyclerView.Adapter<MenuListAdapter.menuListViewHolder> {
Context mContext;
private static final int min = 0;
boolean refreshList;
private RequestManager mGlide;
List<MenuItem> mItems;

class menuListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    CardView cv;
    TextView item_name;
    TextView item_desc;
    TextView price;
    TextView num_orders;
    TextView text_number;
    ImageView item_image;
    Button btn_add;
    Button btn_subtract;
    RelativeLayout rl_item;




    private menuListViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.menulistcv);
        item_name = (TextView)itemView.findViewById(R.id.item_name);
        item_desc = (TextView)itemView.findViewById(R.id.item_desc);
        price = (TextView)itemView.findViewById(R.id.item_price);
        num_orders = (TextView)itemView.findViewById(R.id.item_count);
        item_image = (ImageView)itemView.findViewById(R.id.iv_food);
        btn_add =(Button) itemView.findViewById(R.id.btn_add);
        btn_subtract =(Button) itemView.findViewById(R.id.btn_subtract);
        text_number = (TextView)itemView.findViewById(R.id.text_number);
        rl_item= (RelativeLayout) itemView.findViewById(R.id.item_top);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {


    }
}

public MenuListAdapter(List<MenuItem> menuItems) {
    mItems= menuItems;
}


public MenuListAdapter(Context context , List<MenuItem> menuItems) {
    mItems= menuItems;
    mContext= context;
}

public MenuListAdapter(RequestManager glide, List<MenuItem> menuItems) {
    mGlide = glide;
    mItems= menuItems;
}
public MenuListAdapter() {

}






@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public MenuListAdapter.menuListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.menu_list_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    MenuListAdapter.menuListViewHolder vh = new MenuListAdapter.menuListViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final menuListViewHolder vholder, int position) {
    vholder.item_name.setText(mItems.get(vholder.getAdapterPosition()).getItem_Name());
    vholder.item_desc.setText(mItems.get(vholder.getAdapterPosition()).getDescription());
    vholder.price.setText("$" + String.valueOf(mItems.get(vholder.getAdapterPosition()).getPrice()));
    vholder.text_number.setText(String.valueOf(mItems.get(vholder.getAdapterPosition()).getNum()));
    Log.i("yo",String.valueOf(mItems.get(vholder.getAdapterPosition()).getItem_Name()));
    if(mItems.get(vholder.getAdapterPosition()).getItem_Image() == null){
        mGlide.load(R.drawable.logo)
                .into(vholder.item_image);
    }
    else {
        mGlide.load((mItems.get(vholder.getAdapterPosition()).getItem_Image()))
                .into(vholder.item_image);
    }

    vholder.btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mOnItemClickLister.handlerCarNum(vholder.getAdapterPosition(),1,mItems.get(vholder.getAdapterPosition()),true);

        }


    });
    vholder.btn_subtract.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mOnItemClickLister.handlerCarNum(vholder.getAdapterPosition(),0,mItems.get(vholder.getAdapterPosition()),true);

        }
    });

    vholder.rl_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // Toast.makeText(view.getContext(), "Selected", Toast.LENGTH_SHORT).show();
        }
    });

}

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

private static ModifyItem mOnItemClickLister;

public interface ModifyItem{
     void handlerCarNum(int position,int type, MenuItem menuItem, boolean refreshList);

}

public void setOnItemClickListener(ModifyItem listener) {

    mOnItemClickLister = listener;
}

}

RecyclerviewB适配器

public class SelectedProductAdapter extends RecyclerView.Adapter<SelectedProductAdapter.selectedListViewHolder> {
Context mContext;
MenuListAdapter mMenuListAdapter;
CategoryListAdapter categoryListAdapter;
SparseArray<MenuItem> mItems;
List<MenuItem> mnItems;
private static final int min = 0;
boolean refreshList;


public SelectedProductAdapter(Context context, SparseArray<MenuItem> menuItems) {
    mContext = context;
    mItems = menuItems;

}

public SelectedProductAdapter(Context context, SparseArray<MenuItem> menuItems, MenuListAdapter menuListAdapter) {
    mContext = context;
    mMenuListAdapter = menuListAdapter;
    mItems = menuItems;

}

public SelectedProductAdapter() {

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}


@Override
public SelectedProductAdapter.selectedListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cart_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    SelectedProductAdapter.selectedListViewHolder vh = new SelectedProductAdapter.selectedListViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final selectedListViewHolder vholder, final int position) {
    vholder.tv_name.setText(mItems.valueAt(vholder.getAdapterPosition()).getItem_Name()+ " " + mItems.valueAt(vholder.getAdapterPosition()).getDescription());
    vholder.tv_price.setText("$" + String.valueOf(mItems.valueAt(vholder.getAdapterPosition()).getPrice()));
    vholder.tv_count.setText(String.valueOf(mItems.valueAt(vholder.getAdapterPosition()).getNum()));
    vholder.btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOnItemClickLister.handlerCarNum(vholder.getAdapterPosition(),1,mItems.valueAt(vholder.getAdapterPosition()),true);

        }


    });
    vholder.btn_subtract.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOnItemClickLister.handlerCarNum(vholder.getAdapterPosition(),0,mItems.valueAt(vholder.getAdapterPosition()),true);


        }
    });



}

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

public Object getItem(int position) {
    return mItems.valueAt(position);
}




class selectedListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView tv_price;
    TextView tv_name;
    Button btn_add,btn_subtract;
    TextView tv_count;

    public selectedListViewHolder(View itemView) {
        super(itemView);
        tv_name = (TextView) itemView.findViewById(R.id.tv_name);
        tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        btn_add= (Button) itemView.findViewById(R.id.btn_add);
        btn_subtract= (Button) itemView.findViewById(R.id.btn_subtract);
        tv_count= (TextView) itemView.findViewById(R.id.tv_count);
        itemView.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

    }
}

private static CartItemInterface mOnItemClickLister;

public interface CartItemInterface{
    void handlerCarNum(int position,int type, MenuItem menuItem, boolean refreshList);
}

public void setOnItemClickListener(CartItemInterface listener) {

    mOnItemClickLister = listener;
}

片段/界面添加和删除项目的方法

    //interface method to add items to cart 
@Override
public void handlerCarNum(int position,int type, MenuItem menuItem, boolean refreshList){
    if (type == 0) {
        MenuItem temp = cartItems.get(position);
        if(temp!=null) {
            if (temp == menuItem) {
                if (temp.getNum() < 2) {
                    menuItem.setNum(0);
                    cartItems.remove(position);
                    selectedProductAdapter.notifyItemRemoved(position);
                    selectedProductAdapter.notifyItemRangeChanged(position,cartItems.size());
                    selectedProductAdapter.notifyDataSetChanged();

                } else {
                    int i = menuItem.getNum();
                    menuItem.setNum(--i);



                }
            }
        }



    } else if (type == 1) {
        MenuItem temp = cartItems.get(position);
        if(temp==null){
            menuItem.setNum(1);
            cartItems.append(position, menuItem);

        }else{
            int i= menuItem.getNum();
            menuItem.setNum(++i);

        }

    }
    update(refreshList);

}

//update items  when dataset changes
 @Override
public void update(boolean refreshGoodList){

    if(selectedProductAdapter!=null){
        selectedProductAdapter.notifyDataSetChanged();
    }

    if(menuListAdapter!=null){
        menuListAdapter.notifyDataSetChanged();
    }

    if(bottomSheetLayout.isSheetShowing() && cartItems.size()<1){
        bottomSheetLayout.dismissSheet();
    }
}

0 个答案:

没有答案