我有一个适配器,其中包含一些刷新项目ui的操作。我的问题是刷新具有更改每个项目的逻辑,但是它仅更改屏幕上显示的项目。如果我向下滚动回收站,则屏幕上不存在的项目不会更新。
public class RowOperativaAdapter extends RecyclerView.Adapter<RowOperativaAdapter.ViewHolder> {
private Context context;
private int id;
private OperateModel operateModel;
private OperateModel operateFavoriteModel;
private int fragmentPositionNumber;
private ItemClickListener mClickListener;
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position, int fragmentPosition);
}
// Provide a suitable constructor (depends on the kind of dataset)
public RowOperativaAdapter(Context context, int id, OperateModel operateModel, int fragmentPositionNumber) {
this.context = context;
this.id = id;
this.fragmentPositionNumber = fragmentPositionNumber;
this.operateModel = operateModel;
}
// Provide a reference to the views for each data item
// // Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
public RelativeLayout lay_icon;
public ImageView img_option;
public ImageView optionAddFavorites;
public ImageView optionDelFavorites;
public BMLTextView optionText;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
lay_icon = (RelativeLayout) layout.findViewById(R.id.lay_icon);
img_option = (ImageView) layout.findViewById(R.id.img_option);
optionDelFavorites = (ImageView) layout.findViewById(R.id.iv_del_favorites);
optionAddFavorites = (ImageView) layout.findViewById(R.id.iv_add_favorites);
optionText = (BMLTextView) layout.findViewById(R.id.txt_option_description);
lay_icon.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null){
mClickListener.onItemClick(view, getAdapterPosition(), fragmentPositionNumber);
}
}
}
public boolean addItem(int positionFragment, OperativeItemModel itemModel) {
boolean done = false;
//OperativeItemModel operativeClicked = ((RowOperativaAdapter)context.operateOptionHelper.getListOperateFragments().get(fragmentPosition).getRvSectionOperations().getAdapter()).getItem(position);
if (getFavoritesOperative() == null) {
List<OperativeItemModel> favoritesNull = new ArrayList<OperativeItemModel>();
favoritesNull.add(itemModel);
OperateModel operateModel = new OperateModel(getParentContext().getResources().getString(R.string.operate_favorites), favoritesNull, false, true);
setFavoriteOperative(operateModel);
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).setOperateModel(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getSectionItemHelper().reSetUpFavoriteRV(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyItemInserted(0);
done = true;
} else if (getFavoritesOperative() != null && getFavoritesOperative().getOperatives() == null) {
List<OperativeItemModel> operativesNull = new ArrayList<OperativeItemModel>();
operativesNull.add(itemModel);
getFavoritesOperative().setOperatives(new ArrayList<OperativeItemModel>());
getFavoritesOperative().setOperatives(operativesNull);
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).setOperateModel(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getSectionItemHelper().reSetUpFavoriteRV(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyItemInserted(0);
done = true;
} else if (getFavoritesOperative() != null && getFavoritesOperative().getOperatives() != null) {
if (!getFavoritesOperative().getOperatives().contains(itemModel)) {
List<OperativeItemModel> operativeContains = getFavoritesOperative().getOperatives();
operativeContains.add(itemModel);
getFavoritesOperative().setOperatives(new ArrayList<OperativeItemModel>());
getFavoritesOperative().setOperatives(operativeContains);
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).setOperateModel(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getSectionItemHelper().reSetUpFavoriteRV(getFavoritesOperative());
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyItemInserted(operativeContains.size()-1);
done = true;
}
}
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyDataSetChanged();
return done;
}
public boolean delItem(int positionFragment, OperativeItemModel itemModel) {
boolean done = false;
if (getFavoritesOperative() != null && getFavoritesOperative().getOperatives() != null) {
if (getFavoritesOperative().getOperatives().contains(itemModel)) { // -1 means doesn't contains the model
int index = getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getOperateModel().getOperatives().indexOf(itemModel);
getFavoritesOperative().getOperatives().remove(itemModel);
done = getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getOperateModel().getOperatives().remove(itemModel);
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyItemRemoved(index);
if(getFavoritesOperative() != null && getFavoritesOperative().getOperatives() != null){
if(getFavoritesOperative().getOperatives().size() == 0){
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getSectionItemHelper().setUpFavoritesOperationsRV();
}
}
}
}
getParentContext().getOperateOptionHelper().getListOperateFragments().get(0).getRvSectionOperations().getAdapter().notifyDataSetChanged();
return done;
}
public int position(OperativeItemModel item) {
return getFavoritesOperative().getOperatives().indexOf(item); // -1 if this list does not contain the element
}
public int nextPositionAvailable() {
return operateModel.getOperatives().size();
}
public OperativeItemModel getItem(int position) {
return operateModel.getOperatives().get(position);
}
public long getItemId(int position) {
return position;
}
public int positionInFavorite(OperativeItemModel item){
return operateModel.getOperatives().indexOf(item);
}
// Create new views (invoked by the layout manager)
@Override
public RowOperativaAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.row_operative_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final OperativeItemModel operative = operateModel.getOperatives().get(position);
if (getFragmentPositionNumber() == 0) {
if (getParentContext().isEditingFavorites()) {
if (getFavoritesOperative() != null && getFavoritesOperative().getOperatives() != null) {
if (getFavoritesOperative().getOperatives().contains(operative)) {
drawOperativeIcon(holder, operative, false, true);
} else {
drawOperativeIcon(holder, operative, null, true);
}
} else {
drawOperativeIcon(holder, operative, null, true);
}
} else {
drawOperativeIcon(holder, operative, null, true);
}
} else {
if (getParentContext().isEditingFavorites()) {
if (getFavoritesOperative() != null && getFavoritesOperative().getOperatives() != null) {
if (getFavoritesOperative().getOperatives().contains(operative)) {
drawOperativeIcon(holder, operative, false, false);
} else {
drawOperativeIcon(holder, operative, true, false);
}
} else {
drawOperativeIcon(holder, operative, true, false);
}
} else {
drawOperativeIcon(holder, operative, null, false);
}
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return operateModel.getOperatives().size();
}
public RowOperativaAdapter getAdapterFromFragment(int fragmentPositionNumber) {
return this;
}
public int getFragmentPositionNumber() {
return this.fragmentPositionNumber;
}
public ClientActivity getParentContext() {
return (ClientActivity) context;
}
public OperateModel getFavoritesOperative() {
return getParentContext().getGlobalPositionApiController().getFavoritesSection();
}
public void setFavoriteOperative(OperateModel favoriteOperative) {
getParentContext().getGlobalPositionApiController().setFavoritesSection(favoriteOperative);
}
public void drawOperativeIcon(ViewHolder holder, OperativeItemModel operative, @Nullable Boolean addDelIndicator, Boolean favorite) {
if (addDelIndicator == null) {
holder.optionDelFavorites.setVisibility(View.INVISIBLE);
holder.optionAddFavorites.setVisibility(View.INVISIBLE);
} else if (addDelIndicator) {
holder.optionDelFavorites.setVisibility(View.GONE);
holder.optionAddFavorites.setVisibility(View.VISIBLE);
} else if (!addDelIndicator) {
holder.optionDelFavorites.setVisibility(View.VISIBLE);
holder.optionAddFavorites.setVisibility(View.GONE);
}
holder.img_option.setImageDrawable(CommonUtils.getDrawableByName(holder.img_option.getContext(), operative.getIcon(), favorite, holder.img_option.getContext().getResources().getDrawable(R.drawable.section_operation_mock)));
holder.optionText.setText(operative.getName());
holder.optionText.setSelected(true);
}
}