'Change recyclerview rows after being created
I have a viewPager which contains two fragments. First for showing list of products and another one for showing list of selected products. In the second fragment, I can change some properties of order or remove it. When a product is selected (in the first fragment) a checkbox is checked and its correspond Product is added to an array. I can reflect changes to the first fragment via below interface:
public interface IItemSelectionObserver {
void onItemSelectionChanged(InvoiceItem item, boolean selection_state);
}
The problem is, I update the array of selected products in first fragments but can't update UI. How can I fix UI?
public class FragmentProducts extends Fragment implements IItemSelectionObserver {
private ProductAdapter adapter;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private IItemSelectionObserver observer;
private static ArrayList<InvoiceItem> selectedProducts = new ArrayList<>();
public FragmentProducts setObserver(IItemSelectionObserver observer){
this.observer = observer;
return this;
}
// .
// .
// .
@Override
public void onItemSelectionChanged(InvoiceItem item, boolean selection_state) {
if (selection_state){
adapter.selectedProductChanged(item);
}
else {
adapter.deselectProduct(item);
}
}
}
The adapter is:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private ArrayList<Product> products;
private ArrayList<InvoiceItem> selectedProducts;
private IItemSelectionObserver observer;
private RecyclerView mRecyclerView;
public ProductAdapter(ArrayList<Product> items,
IItemSelectionObserver observer,
ArrayList<InvoiceItem> selectedProducts) {
products = items;
this.observer = observer;
this.selectedProducts = selectedProducts;
}
public void selectedProductChanged(InvoiceItem item)
{
if(selectedProducts.contains(item)){
int index = selectedProducts.indexOf(item);
selectedProducts.get(index).setCount(item.getCount());
notifyDataSetChanged();
}
}
public void deselectProduct(InvoiceItem item){
if(selectedProducts.contains(item)){
selectedProducts.remove(item);
notifyDataSetChanged();
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list_item, parent, false);
return new ProductViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = products.get(position);
holder.setProduct(product);
}
public class ProductViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener,
ChooseQuantityDialog.IChooseQuantityDialogObserver {
public Product mProduct;
public Context context;
public TextView priceTxtView,
nameTxtView,
quantityTxtView;
public ImageView productImageView;
public CheckBox isProductSelected;
public ProductViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
// Init ui
nameTxtView = (TextView) itemView.findViewById(R.id.name);
priceTxtView = (TextView) itemView.findViewById(R.id.price);
quantityTxtView = (TextView) itemView.findViewById(R.id.exist);
productImageView = (ImageView) itemView.findViewById(R.id.image);
isProductSelected = (CheckBox) itemView.findViewById(R.id.product_list_item_chckBx_isSelected);
}
public void setProduct(Product product) {
mProduct = product;
onProductChanged();
}
private void onProductChanged() {
// set view's text and images
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|