我正在使用recyclerview
来创建我的组件,但每个组件都有一个按钮来拍摄或选择照片,因此我需要在此内部recyclerview
中添加照片。但是当我这样做时,添加到正确的位置并需要添加更多的2张照片以显示在列表中。我按照此代码执行此操作code exemple,当我调试时,我看到它插入正确但没有出现正确
我的外部回收者视图适配器:
public class ItemsCategoryInstallationAdapter extends RecyclerView.Adapter<ItemsCategoryInstallationHolder>{
private ArrayList<ItemsInstallation> arrayList;
private SparseBooleanArray expandState = new SparseBooleanArray();
private int positionArray;
private ItemsCategoryInstallationHolder holder;
private SharedPreferences preferences;
private SharedPreferences preferencesItem;
private ArrayAdapter<Integer> quantityAdapter;
private ArrayAdapter<String> coletorAdapter;
private ArrayList<String> content_RFID = new ArrayList<>();
private ArrayList<String> content_RElE = new ArrayList<>();
private ArrayList<String> content_COLETOR = new ArrayList<>();
private int codigo = 0;
private ClickListenerPhoto clickHandler;
public ItemsCategoryInstallationAdapter(ArrayList<ItemsInstallation> data, ClickListenerPhoto clickListenerPhoto){
this.clickHandler = clickListenerPhoto;
if (data != null)
arrayList = new ArrayList<>(data);
else arrayList = new ArrayList<>();
}
@NonNull
@Override
public ItemsCategoryInstallationHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_itemscategoryinstallation, parent, false);
return new ItemsCategoryInstallationHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ItemsCategoryInstallationHolder holder, final int position) {
holder.cb_checkItem.setText(arrayList.get(position).getNome());
holder.btn_dropInstallation.setRotation(expandState.get(position) ? 180f : 0f);
positionArray = position;
preferences = holder.et_number.getContext().getSharedPreferences("Check", Context.MODE_PRIVATE);
preferencesItem = holder.et_number.getContext().getSharedPreferences("ITEM", Context.MODE_PRIVATE);
holder.clickHandler = this.clickHandler;
this.holder = holder;
final boolean alterado = false;
holder.imageGridAdapter.setData(arrayList.get(position).getArrayPhotos());
holder.imageGridAdapter.setRowIndex(position);
holder.btn_addPicItemInst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (clickHandler != null) {
clickHandler.takePictureButton(view, position);
}
}
});
holder.btn_getPicItemInst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (clickHandler != null) {
clickHandler.pickPictureButton(view, position);
}
}
});
/**
* Hide elements that will not be used
*/
if(arrayList.get(position).getCod() == 0){ //coletor
holder.ll_quantityUsed.setVisibility(View.GONE);
holder.ll_type.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
holder.et_number.getText();
getItens(3);
codigo = 0;
}else if (arrayList.get(position).getCod() == 1){ // sondas
holder.ll_type.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
holder.ll_model.setVisibility(View.GONE);
holder.et_chipNumber.setVisibility(View.GONE);
holder.et_number.setVisibility(View.GONE);
callQuantity();
}else if(arrayList.get(position).getCod() == 2){ //RFID
holder.ll_quantityUsed.setVisibility(View.GONE);
holder.ll_type.setVisibility(View.GONE);
holder.et_chipNumber.setVisibility(View.GONE);
holder.et_number.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
getItens(2);
codigo = 2;
}else if (arrayList.get(position).getCod() == 4){ //relé
holder.ll_quantityUsed.setVisibility(View.GONE);
holder.ll_type.setVisibility(View.GONE);
holder.et_chipNumber.setVisibility(View.GONE);
holder.et_number.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
getItens(4);
codigo = 4;
}else if (arrayList.get(position).getCod() == 8){ //lacres do cabo de sonda
holder.ll_type.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
holder.et_chipNumber.setVisibility(View.GONE);
holder.ll_model.setVisibility(View.GONE);
callQuantity();
}else {
holder.ll_quantityUsed.setVisibility(View.GONE);
holder.ll_type.setVisibility(View.GONE);
holder.ll_frequency.setVisibility(View.GONE);
holder.ll_model.setVisibility(View.GONE);
holder.et_chipNumber.setVisibility(View.GONE);
holder.et_number.setVisibility(View.GONE);
}
/**
* Expand card on click
*/
holder.btn_dropInstallation.setOnClickListener(new View.OnClickListener() {
}
});
}
public void insertPhoto(Photo photo){
holder.imageGridAdapter.insertItem(photo);
}
/**
* Get array size
* @return
*/
@Override
public int getItemCount() {
return arrayList.size();
}
/**
* get item id
* @param position
* @return
*/
@Override
public long getItemId(int position) {
return arrayList.get(position).getCod();
}
/**
* get the type of view
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
return position;
}
/**
* Insert item method
* @param user
*/
private void insertItem(ItemsInstallation user) {
arrayList.add(user);
notifyItemInserted(getItemCount());
}
/**
* Update Method
* @param position
*/
public void updateItem(int position, Photo photo) {
holder.imageGridAdapter.insertItem(photo);
Log.e("Qnt", arrayList.get(position).getArrayPhotos().size() + " tamanho - " + position +" Posicao" );
// userModel.incrementAge();
notifyItemChanged(position);
}
/**
* Remove item method
* @param position
*/
private void removerItem(int position) {
arrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, arrayList.size());
}
}
public class ItemsCategoryInstallationHolder extends RecyclerView.ViewHolder{
public ImageButton btn_dropInstallation;
public Button btn_addPicItemInst;
public Button btn_getPicItemInst;
public LinearLayout expandableLayout;
public CheckBox cb_checkItem;
public CardView cardView;
public Spinner sp_frequency;
public Spinner sp_type;
public Spinner sp_model;
public Spinner sp_quantityUsed;
public EditText et_chipNumber;
public EditText et_number;
public LinearLayout ll_frequency;
public LinearLayout ll_type;
public LinearLayout ll_model;
public LinearLayout ll_quantityUsed;
public ClickListenerPhoto clickHandler;
public RecyclerView rv_photosItemsInstallation;
public ImageGridAdapter imageGridAdapter;
public ItemsCategoryInstallationHolder(View itemView) {
super(itemView);
cb_checkItem = (CheckBox) itemView.findViewById(R.id.cb_checkItem);
Context context = itemView.getContext();
rv_photosItemsInstallation = (RecyclerView) itemView.findViewById(R.id.rv_photosItemsInstallation);
rv_photosItemsInstallation.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
imageGridAdapter = new ImageGridAdapter();
rv_photosItemsInstallation.setAdapter(imageGridAdapter);
btn_dropInstallation = (ImageButton) itemView.findViewById(R.id.btn_dropInstallation);
btn_addPicItemInst = (Button) itemView.findViewById(R.id.btn_addPicItemInst);
btn_getPicItemInst = (Button) itemView.findViewById(R.id.btn_getPicItemInst);
expandableLayout = (LinearLayout) itemView.findViewById(R.id.expandableLayout);
cardView = (CardView) itemView.findViewById(R.id.card_view);
sp_frequency = (Spinner) itemView.findViewById(R.id.sp_frequency);
sp_type = (Spinner) itemView.findViewById(R.id.sp_type);
sp_model = (Spinner) itemView.findViewById(R.id.sp_model);
sp_quantityUsed = (Spinner) itemView.findViewById(R.id.sp_quantityUsed);
et_chipNumber =(EditText) itemView.findViewById(R.id.et_chipNumber);
et_number =(EditText) itemView.findViewById(R.id.et_number);
ll_frequency = (LinearLayout) itemView.findViewById(R.id.ll_frequency);
ll_type = (LinearLayout) itemView.findViewById(R.id.ll_type);
ll_model = (LinearLayout) itemView.findViewById(R.id.ll_model);
ll_quantityUsed = (LinearLayout) itemView.findViewById(R.id.ll_quantityUsed);
}
}
我的内部循环视图:
public class ImageGridAdapter extends RecyclerView.Adapter<ImageGridHolder>{
private ArrayList<Photo> photos;
private int mRowIndex = -1;
public ImageGridAdapter(){}
public void setRowIndex(int index) {
mRowIndex = index;
}
public void setData(ArrayList<Photo> data) {
if (photos != data) {
photos = data;
notifyDataSetChanged();
}
}
@NonNull
@Override
public ImageGridHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_itemimage, parent, false);
return new ImageGridHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ImageGridHolder holder, final int position) {
holder.itemView.setTag(position);
try {
holder.itemImage.setImageBitmap(MediaStore.Images.Media.getBitmap(holder.itemImage.getContext().getContentResolver(), photos.get(position).getPathUri()));
} catch (IOException e) {
e.printStackTrace();
}
holder.itemImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.itemImage.getContext().startActivity(new Intent(Intent.ACTION_VIEW, photos.get(position).getPathUri()));
}
});
}
@Override
public int getItemCount() {
return photos.size();
}
@Override
public long getItemId(int position) {
return photos.get(position).getId();
}
@Override
public int getItemViewType(int position) {
return position;
}
public void insertItem(Photo photo) {
photos.add(photo);
photo.setId(Calendar.getInstance().getTimeInMillis());
notifyItemInserted(getItemCount());
}
}
public class ImageGridHolder extends RecyclerView.ViewHolder {
public ImageView itemImage;
public ImageGridHolder(View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.itemImage);
}
}
编辑如果您尝试第三次插入
,此代码有效