【问题标题】:How to set visibility of Android compound view via databinding in Kotlin?如何通过 Kotlin 中的数据绑定设置 Android 复合视图的可见性?
【发布时间】:2018-08-22 04:57:19
【问题描述】:

我检查了很多答案以找到我的问题,但我没有成功。我有一个包含复合可绘制对象的活动。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.my.profile.widgets.ProfileWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    ....
    </LinearLayout>
</layout>

这是我的 ProfileWidget:

  class ProfileWidget @JvmOverloads constructor(context: Context,
                                                  attrs: AttributeSet? = null,
                                                  defStyleAttr: Int = 0
    ) : LinearLayout(context, attrs, defStyleAttr) {

        @Inject lateinit var viewModel: ProfileWidgetViewData
        @Inject lateinit var viewActions: ProfileWidgetActions

        private val binding: WidgetProfileBinding = DataBindingUtil.inflate(
                LayoutInflater.from(context), R.layout.widget_profile, this, true)
    //    private val binding = WidgetProfileBinding.inflate(LayoutInflater.from(context), this, true)

        override fun onAttachedToWindow() {
            super.onAttachedToWindow()
            setupDependencyInjection()
            setupDataBinding()

            viewActions.testUI()
        }

        private fun setupDependencyInjection() {
            (context as ProfileActivity).getProfileComponent()?.inject(this)
        }

        private fun setupDataBinding() {
            binding.viewModel = viewModel
        }
    }

这是它的布局:

 <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="android.view.View" />

            <variable
                name="viewModel"
                type="com.my.profile.widgets.ProfileWidgetViewData" />
        </data>

        <LinearLayout
            android:id="@+id/profilesContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#FF0000"
            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="profile 1"
                android:visibility="@{viewModel.textView_1.get() ? View.VISIBLE : View.INVISIBLE}"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="profile 2"
                android:visibility="@{viewModel.textView_2.get() ? View.VISIBLE : View.INVISIBLE}"/>

        </LinearLayout>

    </layout>

最后我的 ViewModel 类应该制作 TextViews 可见/不可见。

 interface ProfileWidgetViewData {
        val textView_1: ObservableBoolean
        val textView_2: ObservableBoolean
    }

    interface ProfileWidgetActions {
        fun testUI()
    }

    class ProfileWidgetViewModelImpl : ProfileWidgetViewData, ProfileWidgetActions {

        override val textView_1 = ObservableBoolean(false)
        override val textView_2 = ObservableBoolean(false)

        override fun testUI() {
            setProfilesContainerVisibility(true)
            setAddProfileContainerVisibility(true)
        }

        private fun setProfilesContainerVisibility(isVisible: Boolean) {
            textView_1.set(isVisible)
        }

        private fun setAddProfileContainerVisibility(isVisible: Boolean) {
            textView_2.set(isVisible)
        }
    }

不幸的是,我在上面的代码中没有发现任何问题。当我启动 在应用程序中,这两个 TextView 是不可见的,尽管我已将它们设置为可见。

【问题讨论】:

  • 直接使用字段而不调用get(),例如viewModel.textView_1
  • @PrateekBhardwaj:感谢您在这里编辑和改进问题。但是,请不要在代码标题中使用引号块——它们本身不是引号。引号具有特殊的语义含义,即它们是在其他地方说出来或写的,例如演讲、书籍、手册、网站等

标签: android kotlin android-databinding


【解决方案1】:

下面的检查是否在build.gradle中添加(显然你已经添加了)

    apply plugin: 'kotlin-kapt'

    android {
        dataBinding {
            enabled = true
        }     
    }
    dependencies {
       kapt "com.android.databinding:compiler:3.1.3"
    }

并在您的 xml 文件中添加以下行以显示或不可见

 <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="profile 1"
        android:visibility="@{safeUnbox(viewModel.textView_1) ? View.VISIBLE : View.INVISIBLE}"/>

【讨论】:

  • 感谢 Shweta 的回答。我不知道safeUnbox,甚至相关的警告都通过这种方法消失了。但是,我仍然没有看到 TextViews。我确认我有那些 gradle 行。因此,在这种情况下,让我以旧方式启用/禁用视图,以查看它是否有效或仍然无效。我会更新我的问题。
猜你喜欢
  • 2018-03-23
  • 2017-11-09
  • 2021-11-15
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多