例如,里面有一个项目列表RecyclerView。我想在单击相应的列表项后更改项目的图像。如何position在 之后保留项目startActivityForResult,并将图像路由onActivityResult回基于 的对应项目position。
对我来说只是一个选择——保存Activity成员变量的位置,并在之后使用它onActivityResult。也许有更好的方法来避免为创建额外的状态Activity?
代码示例:
public class MyActivity extends AppCompatActivity {
private static final int REQ_TAKE_PICTURE = 1;
// is it possible to avoid this variable as Activity state
private int tempItemPosition;
@Override
public void onCreate(Bundle savedInstanceState) {
...
MyAdapter adapter = new MyAdapter();
adapter.setOnItemClickListener(this::startTakePhoto);
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
if(reqCode == REQ_TAKE_PICTURE) {
updateImageItem(Uri.parse(data.getStringExtra(TakePictureActivity.RESULT_IMAGE_URI)), tempItemPosition)
}
}
private void updateImageItem(Uri imageUri, int position) {
// update item image for position
}
private void startTakePhoto(int position) {
// saving state which is better to avoid
tempItemPosition = position;
Intent intent = new Intent(this, TakePhotoActivity.class);
startActivityForResult(intent, REQ_TAKE_PICTURE);
}
}
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
...
public void setOnItemClickLsitener(OnItemClickListener listener) {
...
}
...
public interface OnItemClickListener {
void onItemClick(int position);
}
}
是否可以调用updateImageItem(Uri, int)after onActivityResult,但不将点击的项目位置保存为Activity状态?
精慕HU
相关分类