DataBinding&LiveData:两种实现(Kotlin和Java),不能使Java产生作用

时间:2019-09-22 10:51:43

标签: android android-databinding android-livedata mutablelivedata

在Java projet中使用DataBinding和LiveData时遇到问题。我在Kotlin上过一门先前的课程,当我尝试实现相同的行为时,我只是使其无法正常工作。我显然在理解方面缺少一些东西,所以我想请您考虑一下。

我将粘贴Kotlin(有效)示例中的代码,然后粘贴Java(无效)示例中的代码。

科特琳

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);
    }
}

说明:在这里,我设置了一个MutableLiveData测试,其值为“ TesT”,然后打算使用android:text =“ @ {noteViewModel.test}”进行显示。但是文本永远不会显示,并且保持空白。

显然这是有问题的,但是尽管这两种实现之间在语法上存在差异,但我仍然无法弄清楚为什么Java版本未在Textview中显示该值。

编辑

由于Rajnish suryavanshi,我的绑定方法不正确,我只能使用:

binding = DataBindingUtil.setContentView(this,R.layout.activity_main);

这将设置具有提供的布局的内容视图并返回绑定。 或者,您可以执行以下操作:

binding = ActivityMainBinding.inflate(getLayoutInflater()); (返回绑定,但未设置内容视图)

setContentView(binding.getRoot()); (使用绑定根视图设置内容视图)

我发现这具有误导性-> https://developer.android.com/topic/libraries/data-binding/expressions

声明我们可以替换

ActivityMainBinding绑定= DataBindingUtil.setContentView(this,R.layout.activity_main);

通过

ActivityMainBinding绑定= ActivityMainBinding.inflate(getLayoutInflater());

这不一样!

很高兴现在能获得微妙之处!

1 个答案:

答案 0 :(得分:1)

看看这两行。

setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());

在扩大版面时没有创建绑定。代替setContentView使用DataBindingUtil.setContentView(this, R.layout.activity_main);

现在您可以使用布局充气器获取视图

binding = ActivityMainBinding.inflate(getLayoutInflater());