在DataBinding使用过程中,一般情况如下
<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"
tools:ignore="RtlSymmetry">
<data>
<variable
name="vm"
type="cn.xxxx.student.ui.state.UserFileShareViewModel" />
<variable
name="onCheckedChangeListener"
type="android.widget.RadioGroup.OnCheckedChangeListener" />
<variable
name="drawerlistener"
type="androidx.drawerlayout.widget.DrawerLayout.DrawerListener" />
<variable
name="onClickListener"
type="android.view.View.OnClickListener" />
</data>
.........
<!-- 跳转页面 -->
<LinearLayout
android:id="@+id/main_top_jump_page_layout"
android:layout_width="140dp"
android:layout_height="0dp"
android:layout_marginVertical="12dp"
android:gravity="center"
visible="@{vm.mainTopJumpPageAreaVisible}"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@+id/tv_main_top_whiteboard_name"
app:layout_constraintTop_toBottomOf="@id/tv_main_top_whiteboard_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/jump_page_to"
android:textColor="@color/c_000000"
android:textSize="12sp" />
<EditText
android:id="@+id/main_page_to_index"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginHorizontal="5dp"
android:layout_weight="1"
android:text="@={vm.mainSelectPageIndex}"
android:background="@drawable/drawable_widget_bg"
android:inputType="number"
android:padding="3dp"
android:textColor="@color/text_black"
android:textSize="12sp" />
<Button
android:id="@+id/btn_jump_page"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:background="@drawable/drawable_widget_bg"
setOnClickListener="@{onClickListener}"
android:text="@string/jump"
android:textSize="12sp" />
</LinearLayout>
......
</layout>
在bindingAdapter中这样写
@BindingAdapter(value = {"setOnClickListener"})
public static void setOnClickListener(View view, View.OnClickListener listener) {
view.setOnClickListener(listener);
}
@BindingAdapter(value = {"addDrawerActionListener"})
public static void addDrawerActionListener(DrawerLayout drawerLayout, DrawerLayout.DrawerListener listener) {
drawerLayout.addDrawerListener(listener);
}
@BindingAdapter(value = {"setOnCheckedChangeActionListener"})
public static void setOnCheckedChangeActionListener(RadioGroup radioGroup, RadioGroup.OnCheckedChangeListener listener) {
radioGroup.setOnCheckedChangeListener(listener);
}
但是当xml界面比较复杂的时候,我们一般会使用include标签,在使用的时候,我们如何把variable 标签的东西传过去
先给layout添加一行
xmlns:app="http://schemas.android.com/apk/res-auto"
然后如下:
<include
android:id="@+id/layout_file_content"
layout="@layout/layout_file_operator_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:onCheckedChangeListener="@{onCheckedChangeListener}"
app:onClickListener="@{onClickListener}"
app:vm="@{vm}" />
这样在include的布局文件里正常写就可以了。
那么自定义view中如何使用呢?
首先:
<!-- 无白板或文件时的底部展示 -->
<cn.xxxx.xxxx.ui.widgets.CenterBackgroundView
android:id="@+id/main_whiteboard_container_background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/view_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:onClickListener="@{onClickListener}" />
然后在自定义view的布局里正常书写
<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"
tools:ignore="RtlSymmetry">
<data>
<variable
name="onClickListener"
type="android.view.View.OnClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f5f8"
android:orientation="vertical">
<TextView
android:id="@+id/tv_center_bottom_whiteboard"
setOnClickListener="@{onClickListener}"
android:layout_width="120dp"
android:layout_height="44dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:background="@drawable/btn_drawable_center"
android:gravity="center"
android:text="@string/wb_shared_wb"
android:textColor="@color/text_blue"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tv_center_bottom_file"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/room_center_bottom_tips" />
</LinearLayout>
</layout>
关键的地方来了
class CenterBackgroundView : ConstraintLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)
: super(context, attrs, defStyleAttr)
var binding: LayoutRoomCenterBackgroundBinding? = null
init {
val inflater = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) as LayoutInflater
binding = DataBindingUtil.inflate(inflater, R.layout.layout_room_center_background, this, true);
}
// 将binding与listener相关联
override fun setOnClickListener(listener: OnClickListener?) { //set方法
binding?.onClickListener = listener
}
}
这样就可以正常使用了