我有一个包含两个片段的viewPager。首先显示产品列表,另一个显示所选产品列表。在第二个片段中,我可以更改顺序的某些属性或将其删除。选择产品(在第一个片段中)时,将选中一个复选框,并将其对应的产品添加到阵列中。我可以通过以下界面反映对第一个片段的更改:
public interface IItemSelectionObserver {
void onItemSelectionChanged(InvoiceItem item, boolean selection_state);
}
问题是,我在第一个片段中更新了所选产品的数组,但无法更新UI。我该如何修复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);
}
}
}
适配器是:
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
}
}
先谢谢。