删除行后更新Recyclerview列表

时间:2018-03-28 06:27:10

标签: android android-recyclerview recycler-adapter

我从这个网站尝试了大多数答案而没有得到理想的结果。

我有recyclerview,其中包含项目列表和每行中包含的按钮。单击该按钮时,我希望删除整行,并自动更新列表。

这是我的适配器类代码:

public class CartAdapter extends RecyclerView.Adapter<CartViewHolder> {

private static final String TAG = CartAdapter.class.getSimpleName();

private final Context context;
private List<Cart> itemsList;
private String id;

public CartAdapter(Context context, List<Cart> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout, parent, false);
    return new CartViewHolder(view);
}

@Override
public void onBindViewHolder(final CartViewHolder viewholder, final int position) {
    final Cart cart = itemsList.get(position);
    id = cart.getProductId();

    AndroidNetworking.get(Constants.PRODUCT_DETAILS_ENDPOINT + id)
            .setTag("Get Product Image")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Cart Item Image Response:\t" + response.toString());
                    String imagePath = null;
                    try {
                        JSONObject itemObject = new JSONObject(response.toString());
                        JSONObject data = itemObject.getJSONObject("data");
                        imagePath = data.getString("image");

                        Picasso.with(context)
                                .load(imagePath)
                                .placeholder(R.drawable.noimage)
                                .into(viewholder.cartItemImg);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(ANError anError) {

                }
            });

    viewholder.cartItemTitle.setText(cart.getProductTitle());
    viewholder.cartQtyTV.setText(cart.getCartQty() + "");
    viewholder.clearProductIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            removeProductRow(cart);
        }
    });
    viewholder.decreaseCartIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            decreaseProduct();
        }
    });
    viewholder.increaseCartIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            increaseProduct();
        }
    });
}

private void increaseProduct() {

}

private void decreaseProduct() {

}

public void removeProductRow(final Cart cart) {
    AndroidNetworking.get(Constants.REMOVE_PRODUCT_CART + id)
            .setTag("Delete Product From Cart")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Delete Product From Cart Response:\t" + response.toString());

                    int position = itemsList.indexOf(cart);
                    itemsList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, itemsList.size());
                }

                @Override
                public void onError(ANError anError) {

                }
            });
}

@Override
public int getItemCount() {
    if (itemsList == null) {
        return 0;
    }
    return itemsList.size();
}
}

此代码有效,项目已删除。但是,在我再次重新访问活动之前,列表不会更新。如何立即更新视图?

2 个答案:

答案 0 :(得分:2)

private update(Cart cart){
      Log.d(TAG, "Delete Product From Cart Response:\t" +
      response.toString());
                int position = itemsList.indexOf(cart);
                itemsList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, itemsList.size());
}

然后,onResponse只需致电update(cart);&lt; - 您可能需要根据自己的需要将其他数据传递到此功能中。

更新(与您的第二个问题相关):

@Override
public long getItemId(int position) {
    return itemList != null ? itemList.get(position).getId() : 0;
}

或者,您可以创建一个公共函数,为您返回完整项目

public Cart getItem(int position){
    return itemList != null itemList.get(position) : null;
} // you have to check if this is null when you call it 

答案 1 :(得分:1)

也许不会在代码下面运行UI线程,导致不及时刷新界面  @覆盖                 public void onResponse(JSONObject response){                     Log.d(标签,“从购物车响应中删除产品:\ t”+ response.toString());

                int position = itemsList.indexOf(cart);
                itemsList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, itemsList.size());
            }