当我将一些数据添加到“列表模型”中并且在旋转设备上恢复保存的数据时,此代码工作正常,不幸的是,在恢复数据并将其设置为在OnRestoreInstanceState方法之后建模,在添加其他数据后,适配器无法使用新添加的数据刷新,适配器中的数据源可以更新,但适配器可以;不知道他们,我使用MVVM数据模型绑定和搜索更多的时间关于这个问题我无法解决.
@H_404_37@解决方法
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); }
@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,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; } }
问题在clickOnRegisterMobileNumber和clickOnSendCommandToRobot方法