Android刷新适配器后再旋转设备工作

这个代码工作正常,当我添加一些数据到'列表模型',并恢复保存在旋转设备上的数据,不幸的是,在恢复数据并将其设置为在onRestoreInstanceState方法内onRestoreInstanceState后,添加其他数据, 适配器无法刷新与新增加的数据,适配器后的数据源可以更新,但适配器可以;不知道他们,我正在使用MVVM数据模型绑定和search更多的时间,我无法解决这个问题。

Rhat有一个简单的提示,旋转设备后,我的适配器可以刷新所有添加的数据哪些适配器不刷新与他们。

 public class ActivityRegister extends BaseActivities{ private List<RobotViewModel> model = new ArrayList<>(); ... @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } @Override protected void onSaveInstanceState(Bundle outState) { outState.putParcelable("model", Parcels.wrap(model)); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle outState) { model = Parcels.unwrap(outState.getParcelable("model")); adapter.setData(model); adapter.notifyItemInserted(model.size() - 1); binding.registerRobot.scrollToPosition(adapter.getItemCount() - 1); } 

添加到model方法:

  @Override public void clickOnSendCommandToRobot() { RobotViewModel temp = new RobotViewModel(); temp.setMessage("message"); temp.setCommand(true); model.add(temp); adapter.notifyItemInserted(model.size() - 1); binding.registerRobot.scrollToPosition(adapter.getItemCount() - 1); } 

我的适配器类:

 public class RobotMessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<RobotViewModel> list; private LayoutInflater inflater; public RobotMessagesAdapter(List<RobotViewModel> robotViewModels) { this.list = robotViewModels; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (inflater == null) { inflater = LayoutInflater.from(parent.getContext()); } if (viewType == SV.RobotMessageType.SENT_BY_USER.ordinal()) { return new UserViewHolder(UserMessagesDataBinding.inflate(inflater, parent, false)); } else { return new RobotViewHolder(RobotDataBinding.inflate(inflater, parent, false)); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { switch (getItemViewType(position)) { case 0: ((UserViewHolder) holder).bind(list.get(position)); break; case 1: ((RobotViewHolder) holder).bind(list.get(position)); break; } } ... public void setData(List<RobotViewModel> data) { Log.e("data size ", data.size() + ""); list.clear(); list.addAll(data); } } 

我想知道为什么这个代码工作正常:

  @Override protected void onRestoreInstanceState(Bundle outState) { model = Parcels.unwrap(outState.getParcelable("model")); adapter.setData(model); adapter.notifyItemInserted(model.size() - 1); binding.registerRobot.scrollToPosition(adapter.getItemCount() - 1); RobotViewModel temp = new RobotViewModel(); temp.setMessage("message"); temp.setCommand(true); model.add(temp); adapter.notifyItemInserted(model.size() - 1); binding.registerRobot.scrollToPosition(adapter.getItemCount() - 1); } 

没有任何问题,在移动这行后:

  RobotViewModel temp = new RobotViewModel(); temp.setMessage("message"); temp.setCommand(true); model.add(temp); adapter.notifyItemInserted(model.size() - 1); binding.registerRobot.scrollToPosition(adapter.getItemCount() - 1); 

到其他方法不起作用:|

我的主持人:

 public class ActivityRegisterPresenter { private ActivityRegisterContract view; public ActivityRegisterPresenter(ActivityRegisterContract mView) { view = mView; } ... } 

和我的ViewModel:

 public class ActivityRegisterViewModel extends BaseObservable { private String readContactPermission; private String getMessages; public ActivityRegisterViewModel() { } @Bindable public String getReadContactPermission() { return readContactPermission; } public void setReadContactPermission(String readContactPermission) { this.readContactPermission = readContactPermission; notifyChange(); } public String getGetMessages() { return getMessages; } public void setGetMessages(String getMessages) { this.getMessages = getMessages; } } 

源代码在这里

问题在clickOnRegisterMobileNumberclickOnSendCommandToRobot方法

Solutions Collecting From Web of "Android刷新适配器后再旋转设备工作"

我的问题解决了,

在将下面这行添加到您的setAdapter特定活动清单后

 android:configChanges="screenSize|orientation|screenLayout" 

但是,如果您devise纵向和横向布局或仅在这两种模式中运行应用程序,它将返回最佳视图。

 The main reason is that, because you called set-data and that method did not immediately notify that the data set was changed, therefore it was not ware. public void setData(List<RobotViewModel> data) { Log.e("data size ", data.size() + ""); list.clear(); list.addAll(data); } 
  public void setData(List<RobotViewModel> data) { Log.e("data size ", data.size() + ""); 
  list.clear(); notifyDataSetChanged(); //here,to signal change occurred. list.addAll(data); notifyDataSetChanged(); //here,to signal change occurred. } should probably get into the habit of notifying changes on the spot. 

主要的问题是,你从来没有通知你所做的更改,你清除它,但你从来没有告诉它,你知道,适配器不知道,你也用addAll插入新的数据,但你还没有通知它。

我试过你的代码,只需要几次更新就可以正常工作。 当你执行这个动作

  model.add(temp); adapter.notifyItemInserted(model.size() - 1); 

您假定对象模型和适配器数据源之间的链接的持久性。 这个假设是错误的,实际上如果你只是用上面的代码replace

  model.add(temp); adapter.setData(model); adapter.notifyItemInserted(model.size() - 1); 

它工作正常。 我希望它有帮助。