我正在开发一款寻宝游戏应用程序,该应用程序会在回收站视图中显示要拍摄的物品列表。该列表是从 Firebase 加载的。有一个 onClickListener,因此当单击列表中的一个项目时,它会打开相机,然后您拍照,我想在所选项目旁边显示图像。现在,当我拍照时,它会在回收视图中显示在多行上。例如,如果我选择列表中的第 0 项并拍照,则该图片将同时显示在列表中的第 0 项和第 11 项上。我试图让它只显示在所选项目上。
我在添加图像后尝试使用 adapter.notifyDataSetChanged() 和 adapter.notifyItemChanged(tmpPosition) 但它们都在多行上显示图像。我知道只有列表中的一项得到更新。我现在的做法是创建一个名为 tmpPosition 的全局 int 变量。当您单击列表中的项目时,我将 tmpPosition 设置为等于所选项目的索引。然后在 onActivityResult 中,我使用以下方法设置图像:
scavengerHuntItemsList.get(tmpPosition).setScavengerHuntImage(imageTaken);
adapter.notifyDataSetChanged();
从我的 MainActivity 这是打开相机的 onClickListener
((ScavengerHuntRecyclerAdapter) dapter).setOnItemClickListener(new
ScavengerHuntRecyclerAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
//Here I am saving the position of the item so later I can update the correct index in the list
tmpPosition = position;
dispatchTakePictureIntent();
}
});
scavengerHuntMainRecyclerView.setAdapter(adapter);
}
我的相机来自 MainActivity
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
photoFile = createPhotoFile();
if (photoFile != null) {
pathToFile = photoFile.getAbsolutePath(); //gets the path to the image
Uri photoURI = FileProvider.getUriForFile(ScavengerHuntMainActivity.this, "com.example.test", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
慕村9548890
相关分类