Android:从 kotlin 合成迁移到数据绑定后无法切换可见性

Android: Can't toggle visibility after migrating from kotlin synthetic to databinding

由于 kotlin synthetic 已被弃用(就像所有东西都无缘无故进入 android),我目前正在努力完全迁移到 databinding。我现在遇到的一个问题是,例如我无法切换导入布局的可见性,它只是说

Unresolved reference: visibility

这甚至说得通,因为我从布局中获取的变量 (calibrateRepairBtnView) 是另一种类型的 binding,它没有函数 visibility

我的问题是:如何在不编写 10 行代码的情况下解决这个问题,我在其中切换 calibrateRepairBtnView 中每个项目的可见性?我在这里遗漏了什么吗?

片段

@AndroidEntryPoint
class CalibrateRepairMessageFragment : BaseCalibrateRepairFragment<FragmentCalibrateRepairMessageBinding>(
    R.layout.fragment_calibrate_repair_message,
) {
    private val calibrateRepairMessageBinding: FragmentCalibrateRepairMessageBinding by viewBinding()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    private fun sucessState() {
        calibrateRepairMessageBinding.calibrateRepairBtnView.visibility // not working anymore
        calibrate_repair_btn_view.visibility = View.VISIBLE // worked before
    }

布局(导入)

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="price"
            type="Float" />
        <variable
            name="btnText"
            type="String" />
        <variable
            name="btnIcon"
            type="android.graphics.drawable.Drawable" />
        <variable
            name="btnTextCapitalized"
            type="Boolean" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/calibrate_btn_background"
            android:layout_width="match_parent"
            android:layout_height="124dp"
            android:background="@color/color_btn_gray"
            android:clickable="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
        </com.google.android.material.appbar.MaterialToolbar>

        <TextView
            android:id="@+id/calibrate_btn_tv_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/standard8dpMargin"
            android:layout_marginEnd="@dimen/big16dpMargin"
            android:text="@string/calibrate_btn_price"
            android:textColor="@color/color_text_dark"
            android:textSize="@dimen/textHeadlineNormal2"
            app:layout_constraintEnd_toStartOf="@+id/calibrate_btn_tv_price"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_background" />

        <TextView
            android:id="@+id/calibrate_btn_tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{price}"
            android:textColor="@color/color_text_blue"
            android:textSize="@dimen/textHeadlineNormal2"
            android:textStyle="bold"
            tools:text="4800.00"
            app:layout_constraintBottom_toBottomOf="@+id/calibrate_btn_tv_sum"
            app:layout_constraintEnd_toStartOf="@+id/calibbrate_btn_tv_intern_curr"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_tv_sum" />

        <TextView
            android:id="@+id/calibbrate_btn_tv_intern_curr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/currency"
            android:textColor="@color/color_text_blue"
            android:textSize="@dimen/textHeadlineNormal2"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/calibrate_btn_tv_price"
            app:layout_constraintEnd_toEndOf="@+id/calibrate_btn_next"
            app:layout_constraintTop_toTopOf="@+id/calibrate_btn_tv_price" />

        <TextView
            android:id="@+id/calibrate_btn_tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="@string/calibrate_btn_desc"
            android:textColor="@color/color_text_gray"
            android:textSize="@dimen/textDescriptionNormal4"
            app:layout_constraintEnd_toEndOf="@+id/calibrate_btn_next"
            app:layout_constraintTop_toBottomOf="@+id/calibrate_btn_tv_price" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/calibrate_btn_next"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_marginStart="@dimen/big16dpMargin"
            android:layout_marginEnd="@dimen/big16dpMargin"
            android:layout_marginBottom="@dimen/standard8dpMargin"
            android:clickable="true"
            android:focusable="true"
            android:text="@{btnText}"
            android:textAllCaps="@{btnTextCapitalized}"
            android:textSize="@dimen/textDescriptionNormal1"
            android:textStyle="normal"
            app:backgroundTint="@color/color_btn_blue"
            app:icon="@{btnIcon}"
            app:iconGravity="textStart"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/calibrate_btn_background" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

布局(片段)

<layout 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">

    <data>
        <import type="com.kofigyan.stateprogressbar.StateProgressBar.StateNumber" />

        <variable
            name="viewModel"
            type="com.example.app.framework.ui.viewmodel.EmailViewModel" />
        <variable
            name="outsideTT"
            type="String" />
    </data>

       <!-- USING THE LAYOUT HERE (binding.calibrateRepairBtnView.visibility) -->
        <include
            android:id="@+id/calibrate_repair_btn_view"
            layout="@layout/app_standard_checkout_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:price="@{viewModel.price}"
            app:btnText="@{@string/calibrate_btn_text}"
            app:btnTextCapitalized="@{true}" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

你可以试试这个

android:visibility="@{viewModel.boolean? View.VISIBLE : View.GONE, default=gone}"

这里我刚刚添加了布尔值,以便您可以定义自己的条件

好的,我已经设法解决了这个问题。我没有做 calibrateRepairMessageBinding.calibrateRepairBtnView.visibility,而是做了 calibrateRepairMessageBinding.calibrateRepairBtnView.root.visibility。正如根所说

Returns the outermost View in the layout file associated with the Binding. If this binding is for a merge layout file, this will return the first root in the merge tag.

有了这个,root object(应该)return最外层的视图,也就是与视图关联的constraintlayout。切换 constrainslayouts 视图会切换整个视图,因此这适合我的情况。

如果有人有更好的答案或认为这可能是错误的,请随时发表评论或提供更好的答案。