MVVM-使用LiveData的自定义选项卡布局控件

时间:2019-01-03 15:11:03

标签: android android-recyclerview android-custom-view android-livedata

我正在尝试使用android mvvm创建自定义标签控件。主要目标是能够绑定当前选定的部分,例如从recyclerView并将其设置在自定义视图上。自定义控件应读取对绑定属性的更改,当有人单击其他选项卡时,将其发送回去。

到目前为止,我决定创建扩展ViewGroup的自定义视图。视图包含代表选项卡控件上当前选定位置的属性。我将那些属性绑定到包含该属性的活动视图模型  类型(LiveData)表示当前在“回收者视图”中当前选中的部分。我包括了部分代码。

这是我到目前为止发现的唯一tutorial,但实际上只涉及自定义视图的简单用法。

我的观点:

<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>

<data>
    <variable
        name="viewModel"
        type="com.example.DeviceMainViewModel"/>
</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.common.deviceMain.DeviceMainActivity">

    <com.example.ScreenToolbar
        android:id="@+id/toolbar_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:tabBackground="@drawable/main_tab_layout_bg"
        app:builtLayout="@{viewModel.toolbarList}"
        app:layoutRes="@layout/toolbar_device"
        app:layout_constraintEnd_toEndOf="parent"
        app:customPosition="@={viewModel.recyclerViewPosition}"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="@{viewModel::testButtonClick}"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sectionRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:adapter="@{viewModel.adapter}"
        android:descendantFocusability="blocksDescendants"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_view" />

</android.support.constraint.ConstraintLayout>

</layout>

自定义视图:

class ScreenToolbar(context: Context, attributeSet: AttributeSet) :ViewGroup(context, attributeSet){

 var currentPosition: Int? = null
        set(value){
            field = value
            Log.d("ScrToolbar", "HIT!!!")
            //do rest of stuff to set curently selected tab
        }
...
}

内部活动视图模型中,我具有mutableLiveData

class DeviceMainViewModel @Inject constructor() : ViewModel() {
    val adapter = SectionsAdapter()
    val recyclerViewPosition : MutableLiveData<Int>
...
}

我希望每次我在viewmodel中更改LiveData时,customView都会在currentPosition的setter中对此做出反应。同样在将onClickListener添加到customView之后,我想在活动viewModel中修改LiveData。到目前为止,代码尚未建立(“找不到属性app:currentPosition的吸气剂),但是我认为整个方法是错误的。

PS。 另外,我不知道如何为MutableLiveData创建bindingAdapters,也许此解决方案需要它。

0 个答案:

没有答案