【问题标题】:Accessing viewbinding object in a non view class?在非视图类中访问视图绑定对象?
【发布时间】:2021-05-29 15:11:07
【问题描述】:

在我的片段中,我使用底部表格来显示一些 UI 组件

这是我的片段 xml

 <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        >
        <!--            other views -->
        <include
            android:id="@+id/feedbackIncluded"
            layout="@layout/chat_feedback" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我目前从我的片段中处理底部表的视图操作或数据。

我打算将用于底页处理的代码分离到另一个类

这是我的底部工作表 xml

<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"
app:behavior_hideable="false"
app:behavior_peekHeight="16dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<data>


</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/parentSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

 <!--        some views -->
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是我创建的要处理的类

class ChatFeedbackBottomSheet(context: Context) {
var binding: ChatFeedbackBinding? = null

init {
     binding = ChatFeedbackBinding.inflate(LayoutInflater.from(context), null, false)
    println(binding?.option1.text)
}

fun openSheet() {
    println("------>" + binding?.option1?.text)
}

}

我似乎不知道如何在此类中获得正确的视图绑定对象?

【问题讨论】:

    标签: android data-binding android-viewbinding


    【解决方案1】:

    你不需要另一个类来重构代码,你可以把函数取出来保存在另一个文件中,你可以在函数参数中传递视图绑定变量并在那里执行你的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      相关资源
      最近更新 更多