Skip to content

Commit

Permalink
docs: 更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Aug 4, 2023
1 parent 1ae4d9a commit 7089ff8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 34 deletions.
32 changes: 7 additions & 25 deletions docs/multi-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,14 @@ rv.linear().setup {

### 接口类型

如果你为addType添加一个接口类型, 然后由可以添加N个其子类作为models的数据. 接口具体实现由不同的子类不同实现
当你`addType<BaseInterfaceModel>`添加一个接口类型, 那么Models中添加的其子类也会被识别为该类型

示例
但当你指定的泛型为抽象类/普通类, 将无法识别为其子类, 请使用`addInterfaceType()`来替代

```kotlin
interface BaseInterfaceModel {
var text: String
}

data class InterfaceModel1(override var text: String) : BaseInterfaceModel

data class InterfaceModel2(val otherData: Int, override var text: String) : BaseInterfaceModel

data class InterfaceModel3(val otherText: String) : BaseInterfaceModel {
override var text: String = otherText
}
```

构建示例数据
示例数据

```kotlin
private fun getData(): List<Any> {
// 在Model中也可以绑定数据
return List(3) { InterfaceModel1("item $it") } +
List(3) { InterfaceModel2(it, "item ${3 + it}") } +
List(3) { InterfaceModel3("item ${6 + it}") }
Expand All @@ -74,15 +59,12 @@ private fun getData(): List<Any> {
声明列表

```kotlin
binding.rv.linear().setup {
addType<BaseInterfaceModel>(R.layout.item_interface_type)
R.id.item.onClick {
toast("点击文本")
}
}.models = getData()
binding.rv.linear().setup {
addType<BaseInterfaceModel>(R.layout.item_interface_type)
}.models = getData()
```

这里只是演示简单的文本, 具体可以编写更加复杂的业务逻辑
仅简单演示, 实际可以让`BaseInterfaceModel`子类分别实现不同业务逻辑


## 区分类型
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package com.drake.brv.sample.ui.fragment

import com.drake.brv.sample.R
import com.drake.brv.sample.databinding.FragmentInterfaceBinding
import com.drake.brv.sample.databinding.FragmentInterfaceTypeBinding
import com.drake.brv.sample.model.BaseInterfaceModel
import com.drake.brv.sample.model.InterfaceModel1
import com.drake.brv.sample.model.InterfaceModel2
import com.drake.brv.sample.model.InterfaceModel3
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.drake.engine.base.EngineFragment
import com.drake.tooltip.toast

class InterfaceFragment : EngineFragment<FragmentInterfaceBinding>(R.layout.fragment_interface) {
class InterfaceTypeFragment : EngineFragment<FragmentInterfaceTypeBinding>(R.layout.fragment_interface_type) {

override fun initView() {
binding.rv.linear().setup {
addType<BaseInterfaceModel>(R.layout.item_interface_type)
R.id.item.onClick {
toast("点击文本")
}
}.models = getData()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.fragment.InterfaceFragment">
tools:context=".ui.fragment.InterfaceTypeFragment">

<TextView
android:layout_width="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/navigation/nav_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
android:label="哔哩哔哩评论" />
<fragment
android:id="@+id/interfaceFragment"
android:name="com.drake.brv.sample.ui.fragment.InterfaceFragment"
android:name="com.drake.brv.sample.ui.fragment.InterfaceTypeFragment"
android:label="接口类型绑定"
tools:layout="@layout/fragment_interface" />
tools:layout="@layout/fragment_interface_type" />

<include app:graph="@navigation/nav_divider" />

Expand Down

0 comments on commit 7089ff8

Please sign in to comment.