数据绑定在这种情况下如何工作

how does databinding work in this scenario

我有一个 activity 及其布局,我正在使用 ActivityQuotesBinding 访问 xml 字段,activity_quotes.xml 没有数据绑定信息。

ActivityQuotesBinding 是如何创建的。

有人可以分享一些关于这个的信息吗

我想为片段做一些类似的事情,但不知道绑定在下面的代码中是如何工作的

提前致谢 R

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.quotes.QuotesActivity">

    <TextView
        android:id="@+id/textView_quotes"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.55"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:scrollbars="vertical"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        tools:text="I like pineapples. - Thomas Jefferson"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/editText_quote"
        android:layout_width="0dp"
        app:layout_constraintWidth_percent="0.7"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:hint="Quote"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_quotes"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/editText_author"
        android:layout_width="0dp"
        app:layout_constraintWidth_percent="0.7"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:hint="Author"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_quote"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button_add_quote"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:backgroundTint="?colorAccent"
        android:text="Add Quote"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@+id/editText_author"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/editText_quote"
        app:layout_constraintTop_toTopOf="@+id/editText_quote"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintWidth_percent="0.25" />
</androidx.constraintlayout.widget.ConstraintLayout>

Activity

class QuotesActivity : AppCompatActivity() {

    private lateinit var binding: ActivityQuotesBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityQuotesBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        initializeUI()
    }

    private fun initializeUI() {
        val factory = InjectorUtils.provideQuotesViewModelFactory()
        val viewModel = ViewModelProviders.of(this, factory)
                .get(QuotesViewModel::class.java)
        viewModel.getQuotes().observe(this, Observer {quotes ->
            val stringBuilder = StringBuilder()
            quotes.forEach {quote ->
                stringBuilder.append("$quote\n\n")
            }
            binding.textViewQuotes.text = stringBuilder.toString()
        })

        binding.buttonAddQuote.setOnClickListener {
            val quote = Quote(binding.editTextQuote.text.toString(), binding.editTextAuthor.text.toString())
            viewModel.addQuotes(quote)
            binding.editTextQuote.setText("")
            binding.editTextAuthor.setText("")
        }

    }
}

您实际上使用的是视图绑定,这与数据绑定完全不同。

当您在模块中指定启用视图绑定时,您的所有布局都将映射到一个 class,它遵循您注意到的名称约定“LayoutfilenameBinding” .

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

因此,要使用视图绑定访问片段布局,您必须执行几乎相同的代码,但有一些差异。

假设您的片段布局是 fragment_layout.xml

private var _binding: FragmentLayoutBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentLayoutBinding.inflate(inflater, container, false)
    val view = binding.root
    return view
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

来源: https://developer.android.com/topic/libraries/view-binding