Skip to content

Commit

Permalink
sample: 优化ViewBindingFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Aug 3, 2023
1 parent 47c95b9 commit 9a7148f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,12 @@ package com.drake.brv.sample.model

import com.drake.brv.BindingAdapter
import com.drake.brv.item.ItemBind
import com.drake.brv.sample.R
import com.drake.brv.sample.databinding.ItemSimpleBinding

class SimpleBindingModel : ItemBind {
class SimpleBindingModel() : ItemBind {

// 多类型列表请注意区分类型 https://liangjingkanji.github.io/BRV/multi-type.html#_5
override fun onBind(holder: BindingAdapter.BindingViewHolder) {
// 不推荐这种方式, 因为Model只应该存在数据和逻辑, 如果包含UI绑定会导致视图耦合不例如项目迭代 (例如BRVAH)
// val appName = holder.context.getString(R.string.app_name)

// 使用不同的方法来获取视图控件
// holder.findView<TextView>(R.id.tv_simple).text = appName // 使用findById
// val dataBinding = holder.getBinding<ItemMultiTypeOneBinding>() // 使用DataBinding或ViewBinding

// 获取数据对象
// 如果存在多种数据类型, 请使用holder.getModelOrNull<Data>()或者if来判断itemViewType类型, 避免取值类型转换错误
// val data = holder.getModel<Data>()

when (holder.itemViewType) {
R.layout.item_simple_text -> {
val binding = holder.getBinding<ItemSimpleBinding>()
val data = holder.getModel<SimpleModel>()
binding.tv.text = data.name
}
}

// val binding = holder.getBinding<ItemViewBindingBinding>()
// binding.tvSimple.text = holder.layoutPosition.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package com.drake.brv.sample.ui.fragment
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.drake.brv.sample.R
import com.drake.brv.sample.databinding.FragmentViewBindingBinding
import com.drake.brv.sample.databinding.ItemCommentBinding
import com.drake.brv.sample.databinding.ItemSimpleTextBinding
import com.drake.brv.sample.databinding.ItemViewBindingBinding
import com.drake.brv.sample.model.SimpleBindingModel
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
Expand All @@ -22,22 +20,10 @@ class ViewBindingFragment : Fragment(R.layout.fragment_view_binding) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.rv.linear().setup {
addType<SimpleBindingModel>(R.layout.item_simple_text)
addType<SimpleBindingModel>(R.layout.item_view_binding)
onBind {

// 单一类型不用判断
val binding = getBinding<ItemSimpleTextBinding>() // 使用ViewBinding/DataBinding都可以使用本方法
val binding = getBinding<ItemViewBindingBinding>()
binding.tvSimple.text = layoutPosition.toString()

// 如果是多类型可以通过判断ViewBinding类型分开处理
when (val viewBinding = getBinding<ViewBinding>()) {
is ItemSimpleTextBinding -> {
viewBinding.tvSimple.text = layoutPosition.toString()
}
is ItemCommentBinding -> {
viewBinding.tvContent.text = layoutPosition.toString()
}
}
}
R.id.tv_simple.onClick {
toast("点击文本")
Expand Down
44 changes: 44 additions & 0 deletions sample/src/main/res/layout/item_view_binding.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ MIT License
~
~ Copyright (c) 2023 劉強東 https://github.com/liangjingkanji
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="16dp"
android:background="@drawable/bg_card"
android:foreground="?selectableItemBackgroundBorderless">

<TextView
android:id="@+id/tv_simple"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="italic"
tools:text="Simple Text" />

</FrameLayout>

0 comments on commit 9a7148f

Please sign in to comment.