最近使用RecyclreView写了一个列表,并且点击使用startActivityForResult()进入详情界面,在详情页面中实现删除这个条目(position)的操作 或者 编辑详情的内容操作(也就是改变了列表中展示的数据),然后返回时使用setResult()将(position)数据传回来,再使用notifyItemRemoved(position) 或者notifyItemChanged(position) 进行刷新,但是出问题了。
如果是删除操作 ,item的位置并没有发生改变,或者位置发生错乱,很是奇怪诡异, 然后查了下资料才发现错误:在使用notifyItemRemoved()这个方法时,由于该方法不会使position及其之后位置的itemView重新onBindViewHolder,重新刷新后面的数据会导致下标错乱。有可能产生一些坑爹的bug。
解决方法就是:使notifyItemRangeChanged(int pisitionStar , int itemCount);
如果是编辑操作,那么只需要将你改变的bean中的字段的新内容赋给原来的字段就可以了,然后使用notifyItemChanged(posiion);进行刷新。
以下就是关键代码:
删除的返回:
// TODO: 2017/5/14 通知列表页面删除成功了
Intent oa_share = new Intent();
oa_share.putExtra("position", position);
oa_share.putExtra("share_flag", share_flag);
setResult(JHConstants.OA_SHARE_SHAREDETAIL, oa_share);
finish();
编辑或改变列表状态的返回:
/**
* 点击手机返回键
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK) {
if ("0".equals(share_flag)) {//编辑
Intent oa_share = new Intent();
oa_share.putExtra("position", position);
oa_share.putExtra("share_flag", share_flag);
oa_share.putExtra("content", content);
oa_share.putExtra("attachments", (Serializable) attachments);
setResult(JHConstants.OA_SHARE_SHAREDETAIL, oa_share);
finish();
} else if ("2".equals(share_flag)) {//回执成功
Intent oa_share = new Intent();
oa_share.putExtra("share_flag", share_flag);
oa_share.putExtra("position", position);
oa_share.putExtra("as_receipt_status", shareDetail.as_receipt_status);
oa_share.putExtra("as_is_receipt", shareDetail.as_is_receipt);
setResult(JHConstants.OA_SHARE_SHAREDETAIL, oa_share);
finish();
}
}
return super.onKeyDown(keyCode, event);
}
以下为接收的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case JHConstants.OA_SEND_SHARE_REQUESTCODE://发分享,进入详情
if (resultCode == JHConstants.OA_SHARE_SENDSHARE) {//发分享成功返回
pageIndex = 1;
onLoadData(pageIndex);
} else if (resultCode == JHConstants.OA_SHARE_SHAREDETAIL) {//0 编辑,1 删除分享,2回执状态
String share_flag = data.getStringExtra("share_flag");
int position_change = data.getIntExtra("position", -1);
String content = data.getStringExtra("content");
String as_receipt_status = data.getStringExtra("as_receipt_status");
String as_is_receipt = data.getStringExtra("as_is_receipt");
if ("0".equals(share_flag)) {
shareListCodeList.get(position_change).content = content;
List<OA_Share_DetailBean.ShareDetailBean.AttachmentsBean> getAttachments
= (List<OA_Share_DetailBean.ShareDetailBean.AttachmentsBean>) data.getSerializableExtra("attachments");
OA_ShareListBean.ShareListCodeBean.AttachmentBean bean = null;
List<OA_ShareListBean.ShareListCodeBean.AttachmentBean> attachments = new ArrayList<>();
if (getAttachments != null) {
for (OA_Share_DetailBean.ShareDetailBean.AttachmentsBean attachmentBean : getAttachments) {
bean = new OA_ShareListBean.ShareListCodeBean.AttachmentBean();
bean.attachment = attachmentBean.attachment;
attachments.add(bean);
}
}
shareListCodeList.get(position_change).attachments = attachments;
oa_shareadapter.notifyItemChanged(position_change);
} else if ("1".equals(share_flag)) {
shareListCodeList.remove(position_change);
oa_shareadapter.notifyItemRemoved(position_change);
if(position_change != shareListCodeList.size()){
oa_shareadapter.notifyItemRangeChanged(position_change, shareListCodeList.size() - position_change);
}
}else if("2".equals(share_flag)){
shareListCodeList.get(position_change).as_receipt_status = as_receipt_status;
shareListCodeList.get(position_change).as_is_receipt = as_is_receipt;
oa_shareadapter.notifyItemChanged(position_change);
}
}
break;
}
}
好啦!!这样问题就解决了!!
在下刚刚写博客,如有不满意还请多多包含。