我有两个recyclerview(rvone,rvtwo) 这个rvone是显示数据列表的那个 单击rvone项时,数据值减小 这个项目的一个例子
标题 Rp.1000 X 4个 总共4000
好吧,如果单击它,必须 标题 Rp.1000 X 3个 总共3000 难道不是只减了一个,而是搬到了rvtwo 表示结果是这样
标题 Rp.1000 X 1个 总共1000
好吧,该功能已经在运行,但是每次单击它时,即使是现有的新功能也不是新的
这是一个例子 标题 Rp.1000 X 4个 总共4000 //减少了点击次数,并且在rvtwo上显示了缺失
//表示像这样 标题 Rp.1000 X 1个 总共1000
并且rvone变成 标题 Rp.1000 X 3个 总共3000
当我再次单击时,它变成这样 rvone: 标题 Rp.1000 X 2个 共2000张
rvtwo: 标题 Rp.1000 X 1个 总共1000
标题 Rp.1000 X 1个 总共1000
它变成了两个项目,而不是之前覆盖它 注意:我只添加构造函数和bindViewHolder的代码
这是我的rvone_adapter函数onclick将数据发送到rvtwo
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {
List<OrderModel> orderModels;
public List<OrderCatchModel> orderCatchModels = new ArrayList<>();
List<OrderDetailModel> orderDetailModels;
Context context;
OrderCatchAdapter orderCatchAdapter;
OrderActivity orderActivity;
public OrderAdapter(Context context, List<OrderModel> orderModels,List<OrderDetailModel> orderDetailModels){
this.context = context;
this.orderModels = orderModels;
this.orderDetailModels = orderDetailModels;
}
@Override
public void onBindViewHolder(@NonNull final OrderViewHolder holder, final int position) {
final OrderModel orderModel = orderModels.get(0);
final Long id = orderModel.getOrderDetailModels().get(position).getId();
final String itemName = orderModel.getOrderDetailModels().get(position).getItem();
final long price = orderModel.getOrderDetailModels().get(position).getPrice();
final long quantity = orderModel.getOrderDetailModels().get(position).getQuantity();
//final long amount = orderModel.getOrderDetailModels().get(position).getAmount();
holder.mTxt_item.setText(orderModel.getOrderDetailModels().get(position).getItem());
holder.mTxt_price.setText("Rp. " + orderModel.getOrderDetailModels().get(position).getPrice());
holder.mTxt_quantity.setText(orderModel.getOrderDetailModels().get(position).getQuantity() + " Pcs");
holder.mTxt_amount.setText("Rp. " + orderModel.getOrderDetailModels().get(position).getAmount());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quantity == 0){
orderModels.get(0).getOrderDetailModels().remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,orderModels.get(0).getOrderDetailModels().size());
}else {
long q1 = quantity - 1;
long a1 = q1 * price;
orderModel.getOrderDetailModels().get(position).setQuantity(q1);
orderModel.getOrderDetailModels().get(position).setAmount(a1);
notifyDataSetChanged();
OrderCatchModel orderCatchModel = new OrderCatchModel();
orderCatchModel.setId(id);
orderCatchModel.setItem(itemName);
orderCatchModel.setPrice(price);
orderCatchModel.setQuantity(quantity - q1);
orderCatchModel.setAmount((quantity - q1) * price);
orderCatchModels.add(orderCatchModel);
if (context instanceof OrderActivity) {
((OrderActivity)context).addItemToRvTwo(orderCatchModels, orderModel.getOrderDetailModels(), orderModels);
}
}
}
});
}
这是我的rvtwo_adapter函数onclick,用于将数据从rvtwo发送回rvone
public OrderCatchAdapter(Context context, List<OrderCatchModel> orderCatchModel, List<OrderDetailModel> orderDetailModels, List<OrderModel> orderModels){
this.context = context;
this.orderDetailModels = orderDetailModels;
this.orderCatchModels = orderCatchModel;
this.orderModels = orderModels;
}
@Override
public void onBindViewHolder(@NonNull OrderCatchViewHolder holder, final int position) {
final OrderCatchModel orderCatchModel = orderCatchModels.get(position);
final Long id = orderCatchModel.getId();
final String itemName = orderCatchModel.getItem();
final long price = orderCatchModel.getPrice();
final long quantity = orderCatchModel.getQuantity();
holder.mTxt_item.setText(orderCatchModel.getItem());
holder.mTxt_price.setText("Rp. " + orderCatchModel.getPrice());
holder.mTxt_quantity.setText(orderCatchModel.getQuantity() + " Pcs");
holder.mTxt_amount.setText("Rp. " + orderCatchModel.getAmount());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quantity <= 0){
orderCatchModels.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,orderCatchModels.size());
notifyDataSetChanged();
}else {
long q1 = quantity - 1;
long a1 = q1 * price;
orderCatchModel.setQuantity(q1);
orderCatchModel.setAmount(a1);
notifyDataSetChanged();
// Long idItem = orderModels.get(0).getOrderDetailModels().get(position).getId();
OrderDetailModel orderDetailModel = new OrderDetailModel();
orderDetailModel.setId(id);
orderDetailModel.setItem(itemName);
orderDetailModel.setPrice(price);
orderDetailModel.setQuantity(quantity - q1);
orderDetailModel.setAmount((quantity - q1) * price);
orderDetailModels.add(orderDetailModel);
if (context instanceof OrderActivity) {
((OrderActivity)context).addDataToRvOne(orderModels, orderDetailModels);
}
}
}
});
}
这是我的活动
public void addDataToRvOne(List<OrderModel> orderModels, List<OrderDetailModel> orderDetailModels) {
orderAdapter = new OrderAdapter(this, orderModels,orderDetailModels);
LinearLayoutManager linearLayoutManagerRvOne = new LinearLayoutManager(this);
rv_one.setLayoutManager(linearLayoutManagerRvOne);
rv_one.setAdapter(orderAdapter);
orderAdapter.notifyItemInserted(itemPosition);
itemPosition += 1;
}
public void addItemToRvTwo(List<OrderCatchModel> orderCatchModels, List<OrderDetailModel> orderDetailModels, List<OrderModel> orderModels) {
orderCatchAdapter = new OrderCatchAdapter(this, orderCatchModels,orderDetailModels, orderModels);
LinearLayoutManager linearLayoutManagerRvTwo = new LinearLayoutManager(this);
rv_two.setLayoutManager(linearLayoutManagerRvTwo);
rv_two.setAdapter(orderCatchAdapter);
orderCatchAdapter.notifyItemInserted(itemPosition);
itemPosition += 1;
}
此模型抛出项目rv_one
public class OrderDetailModel implements Serializable {
private Long id;
private String item;
private long price;
private long quantity;
private long amount;
public OrderDetailModel(
Long id,
String item,
long price,
long quantity
) {
this.id = id;
this.item = item;
this.price = price;
this.quantity = quantity;
this.amount = price * quantity;
}
public OrderDetailModel() {
}
此模型rv_two捕获项目rv_one
public class OrderCatchModel implements Serializable {
private Long id;
private String item;
private long price;
private long quantity;
private long amount;
public OrderCatchModel(Long id, String item, long price, long quantity, long amount) {
this.id = id;
this.item = item;
this.price = price;
this.quantity = quantity;
this.amount = amount;
}
public OrderCatchModel() {
}
这是虚拟数据
@SuppressLint("SimpleDateFormat")
public class MockData {
public static List<OrderModel> ORDER_MODELS;
static {
ORDER_MODELS = new ArrayList<>();
try {
order(
1L,
"#00000001-001",
new SimpleDateFormat("dd-MM-yyyy HH:mm").parse("21-08-2019 09:47"),
null,
"MJ002",
"Sugiyono",
Arrays.asList(
new OrderDetailModel(
1L,
"Nasi Goreng",
10000,
2
),
new OrderDetailModel(
2L,
"Bubur Ayam",
9000,
4
),
new OrderDetailModel(
3L,
"Bubur Kacang",
5000,
5
),
new OrderDetailModel(
4L,
"Jus Alpukat",
8000,
6
),
new OrderDetailModel(
5L,
"Jus Durian",
15000,
2
),
new OrderDetailModel(
6L,
"Jus Apel",
13000,
3
)
)
);
} catch (Exception ex) {
Log.wtf(MockData.class.getSimpleName(), ex);
}
}
@SuppressWarnings("SameParameterValue")
private static void order(
Long id,
String number,
Date date,
String customer,
String table,
String staff,
List<OrderDetailModel> orderDetailModels
) {
OrderModel orderModel = new OrderModel();
orderModel.setId(id);
orderModel.setNumber(number);
orderModel.setDate(date);
orderModel.setCustomer(customer);
orderModel.setTable(table);
orderModel.setStaff(staff);
orderModel.getOrderDetailModels().addAll(orderDetailModels);
ORDER_MODELS.add(orderModel);
}
}
如果需要其他代码,请问我