gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:03:54 24 4
gpt4 key购买 nike

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>
布局( fragment )
<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>

最佳答案

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

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


有了这个, root object (应该)返回最外层的 View ,即 constraintlayout与 View 相关联。切换 constrainslayouts view 会切换整个 View ,因此这适合我的情况。
如果有人有更好的答案或认为这可能是错误的,请随时发表评论或提供更好的答案。

关于Android:从 kotlin 合成迁移到数据绑定(bind)后无法切换可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65187772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com