首页 > 解决方案 > 导航组件 - 自定义视图状态被最后一个视图覆盖

问题描述

我面临着奇怪的行为,我使用导航组件而不使用实时数据。我已经创建了自定义视图(复合视图)并在需要的地方添加了这个视图。例如

public class InputCompoundView extends ConstraintLayout {
  private ViewInputTextBinding binding;

  public InputCompoundView(Context context) {
      this(context, null);
  }

  public InputCompoundView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public InputCompoundView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_input_text, this, true);
  }
}

在片段布局中,我根据字段多次使用上述自定义视图,例如名字、姓氏和电子邮件。视图显示正常。但问题是当我导航到下一个片段并再次回到这个片段时,其他两个视图值被替换为电子邮件值。

注意- 这个问题与不使用 liveData 无关,仅供参考我也尝试在 onViewCreated 上设置硬编码值,但即使这些值也被替换为电子邮件值。

仅供参考,而如果我不使用自定义视图,只需在片段布局中添加 view_input_text 布局,似乎工作正常。

标签: androidnavigationandroid-custom-view

解决方案


推荐阅读