DataBinding 和 LiveData:两个实现(Kotlin 和 Java)并且无法使 Java impl 工作
DataBinding & LiveData : two implementations (Kotlin & Java) and can't make the Java impl working
我在 Java 项目中使用 DataBinding 和 LiveData 时遇到问题。我学习了之前的 Kotlin 课程,当我尝试实现相同的行为时,我就是无法让它发挥作用。我显然在理解方面遗漏了一些东西,所以我想听听你的想法。
我将粘贴 Kotlin(有效)示例中的代码,然后粘贴 Java(无效)示例中的代码。
KOTLIN
score_fragment.xml
...
<data>
<variable
name="scoreViewModel"
type="com.example.android.guesstheword.screens.score.ScoreViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/score_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.score.ScoreFragment">
<TextView
android:id="@+id/score_text"
...
android:text="@{String.valueOf(scoreViewModel.score)}"
.../>
...
ScoreFragment.kt
class ScoreFragment : Fragment() {
private lateinit var viewModelFactory: ScoreViewModelFactory
private lateinit var viewModel: ScoreViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate view and obtain an instance of the binding class.
val binding: ScoreFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.score_fragment,
container,
false
)
// Get args using by navArgs property delegate
val scoreFragmentArgs by navArgs<ScoreFragmentArgs>()
viewModelFactory = ScoreViewModelFactory(scoreFragmentArgs.score)
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ScoreViewModel::class.java)
binding.scoreViewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}
}
ScoreViewModel.kt
class ScoreViewModel(finalScore: Int) : ViewModel() {
private val _score = MutableLiveData<Int>()
val score: LiveData<Int>
get() = _score
private val _eventPlayAgain = MutableLiveData<Boolean>()
val eventPlayAgain: LiveData<Boolean>
get() = _eventPlayAgain
init {
Timber.i("ScoreViewModel created")
_score.value = finalScore
}
fun onPlayAgain() {
_eventPlayAgain.value = true
}
fun onPlayAgainComplete() {
_eventPlayAgain.value = false
}
override fun onCleared() {
super.onCleared()
Timber.i("ScoreViewModel cleared")
}
}
说明 : 让我们只关注分数值。在 ScoreViewModel 中,该值属于 LiveData 类型。当片段启动时,该值通过“@{String.valueOf(scoreViewModel.score)}”正确显示在屏幕上。这工作正常。
JAVA
activity_main.xml
<data>
<variable
name="noteViewModel"
type="com.example.architectureapp.viewModel.NoteViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:text="@{noteViewModel.test}" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private NoteViewModel mNoteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
mNoteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
binding.setNoteViewModel(mNoteViewModel);
binding.setLifecycleOwner(this);
}
}
NoteViewModel
public class NoteViewModel extends AndroidViewModel {
public MutableLiveData<String> test = new MutableLiveData<>("TesT");
public NoteViewModel(@NonNull Application application) {
super(application);
}
}
说明 :这里我设置了一个值为 "TesT" 的 MutableLiveData 测试,然后我打算使用 android:text="@ 显示它{noteViewModel.test}”。但是文本永远不会显示并保持空白。
显然有问题,但尽管两种实现之间存在语法差异,但我还是无法弄清楚为什么 Java 版本没有在 Textview 中显示值。
编辑
感谢 Rajnish suryavanshi,我没有以正确的方式绑定,我不得不只使用 :
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
这个设置内容视图与提供的布局和 return 绑定。
或者你可以这样做:
binding = ActivityMainBinding.inflate(getLayoutInflater());(return绑定但不设置内容视图)
setContentView(binding.getRoot());(用绑定根视图设置内容视图)
我发现这具有误导性 -> https://developer.android.com/topic/libraries/data-binding/expressions
说明我们可以替换
ActivityMainBinding 绑定 = DataBindingUtil.setContentView(this, R.layout.activity_main);
来自
ActivityMainBinding 绑定 = ActivityMainBinding.inflate(getLayoutInflater());
这不一样!
很高兴现在得到了敏锐度!
看看这两行。
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
您在扩充布局时并未创建绑定。而不是 setContentView
使用 DataBindingUtil.setContentView(this, R.layout.activity_main);
现在您可以使用 layout inflater 获取视图
binding = ActivityMainBinding.inflate(getLayoutInflater());
我在 Java 项目中使用 DataBinding 和 LiveData 时遇到问题。我学习了之前的 Kotlin 课程,当我尝试实现相同的行为时,我就是无法让它发挥作用。我显然在理解方面遗漏了一些东西,所以我想听听你的想法。
我将粘贴 Kotlin(有效)示例中的代码,然后粘贴 Java(无效)示例中的代码。
KOTLIN
score_fragment.xml
...
<data>
<variable
name="scoreViewModel"
type="com.example.android.guesstheword.screens.score.ScoreViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/score_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.score.ScoreFragment">
<TextView
android:id="@+id/score_text"
...
android:text="@{String.valueOf(scoreViewModel.score)}"
.../>
...
ScoreFragment.kt
class ScoreFragment : Fragment() {
private lateinit var viewModelFactory: ScoreViewModelFactory
private lateinit var viewModel: ScoreViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate view and obtain an instance of the binding class.
val binding: ScoreFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.score_fragment,
container,
false
)
// Get args using by navArgs property delegate
val scoreFragmentArgs by navArgs<ScoreFragmentArgs>()
viewModelFactory = ScoreViewModelFactory(scoreFragmentArgs.score)
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ScoreViewModel::class.java)
binding.scoreViewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}
}
ScoreViewModel.kt
class ScoreViewModel(finalScore: Int) : ViewModel() {
private val _score = MutableLiveData<Int>()
val score: LiveData<Int>
get() = _score
private val _eventPlayAgain = MutableLiveData<Boolean>()
val eventPlayAgain: LiveData<Boolean>
get() = _eventPlayAgain
init {
Timber.i("ScoreViewModel created")
_score.value = finalScore
}
fun onPlayAgain() {
_eventPlayAgain.value = true
}
fun onPlayAgainComplete() {
_eventPlayAgain.value = false
}
override fun onCleared() {
super.onCleared()
Timber.i("ScoreViewModel cleared")
}
}
说明 : 让我们只关注分数值。在 ScoreViewModel 中,该值属于 LiveData 类型。当片段启动时,该值通过“@{String.valueOf(scoreViewModel.score)}”正确显示在屏幕上。这工作正常。
JAVA
activity_main.xml
<data>
<variable
name="noteViewModel"
type="com.example.architectureapp.viewModel.NoteViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:text="@{noteViewModel.test}" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private NoteViewModel mNoteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
mNoteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
binding.setNoteViewModel(mNoteViewModel);
binding.setLifecycleOwner(this);
}
}
NoteViewModel
public class NoteViewModel extends AndroidViewModel {
public MutableLiveData<String> test = new MutableLiveData<>("TesT");
public NoteViewModel(@NonNull Application application) {
super(application);
}
}
说明 :这里我设置了一个值为 "TesT" 的 MutableLiveData 测试,然后我打算使用 android:text="@ 显示它{noteViewModel.test}”。但是文本永远不会显示并保持空白。
显然有问题,但尽管两种实现之间存在语法差异,但我还是无法弄清楚为什么 Java 版本没有在 Textview 中显示值。
编辑
感谢 Rajnish suryavanshi,我没有以正确的方式绑定,我不得不只使用 :
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
这个设置内容视图与提供的布局和 return 绑定。 或者你可以这样做:
binding = ActivityMainBinding.inflate(getLayoutInflater());(return绑定但不设置内容视图)
setContentView(binding.getRoot());(用绑定根视图设置内容视图)
我发现这具有误导性 -> https://developer.android.com/topic/libraries/data-binding/expressions
说明我们可以替换
ActivityMainBinding 绑定 = DataBindingUtil.setContentView(this, R.layout.activity_main);
来自
ActivityMainBinding 绑定 = ActivityMainBinding.inflate(getLayoutInflater());
这不一样!
很高兴现在得到了敏锐度!
看看这两行。
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
您在扩充布局时并未创建绑定。而不是 setContentView
使用 DataBindingUtil.setContentView(this, R.layout.activity_main);
现在您可以使用 layout inflater 获取视图
binding = ActivityMainBinding.inflate(getLayoutInflater());