[databinding]RecyclerView+databinding+kotlin
程序员文章站
2022-03-08 20:49:33
...
一.数据:
User:
class User { var firstname: ObservableField<String> = ObservableField() var lastname: ObservableField<String> = ObservableField() var isAdult: ObservableBoolean = ObservableBoolean() var displayName: ObservableField<String> = ObservableField() var age: ObservableField<Int> = ObservableField() }
二.item view:
recycler_view_item.xml:
<layout> <data> <variable name="user" type="com.m.databindingdemo.model.User"/> </data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginBottom="10dp" android:background="@android:color/darker_gray" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstname}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(user.age)}"/> </LinearLayout> </layout>
三.Adapter:
class RecyclerViewAdapter(private var userList: MutableList<User>) : RecyclerView.Adapter<RecyclerViewAdapter.TestViewHolder>() { override fun getItemCount(): Int = userList.size override fun onBindViewHolder(holder: TestViewHolder?, position: Int) { holder?.bind(userList[position]) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): TestViewHolder { val binding = DataBindingUtil.inflate<RecyclerViewItemBinding>( LayoutInflater.from(parent?.context), R.layout.recycler_view_item, parent, false ) return TestViewHolder(binding) } class TestViewHolder(private val binding: RecyclerViewItemBinding) : ViewHolder(binding.root) { fun bind(data: User) { //方法一: // binding.setVariable(BR.user,data) //方法二: binding.user = data binding.executePendingBindings() } } }
四.Activity:
class RecycleViewActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_recycle_view) val layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = layoutManager recycler_view.adapter = RecyclerViewAdapter(initUsers()) } private fun initUsers() : MutableList<User>{ val userList = mutableListOf<User>() for (i in 1..5) { var user = User() user.firstname.set("name $i") user.age.set(i) userList.add(user) } return userList } }
参考:
1.java+RecyclerView:https://www.cnblogs.com/DoNetCoder/p/7243878.html?utm_source=tuicool&utm_medium=referral
2.kotlin+RecyclerView:https://github.com/JetBrains/kotlin-examples/blob/master/gradle/android-databinding/app/src/main/java/org/example/kotlin/databinding/OtherActivity.kt
上一篇: 使用kotlin 分页的另一种代码实现
推荐阅读
-
Android DataBinding的官方双向绑定示例
-
C#数据绑定(DataBinding)简单实现方法
-
databinding原理面试(底层原理剖析系列)
-
databinding原理面试(底层原理剖析系列)
-
android使用DataBinding来设置空状态
-
C#数据绑定(DataBinding)简单实现方法
-
Android 用Databinding写recyclerview的adapter
-
DataBinding onClick的七种点击方式
-
DataBinding防止多次点击 DataBinding防误触 DataBinding点击防抖
-
AbstractMethodError: abstract method "androidx.databinding.ViewDataBinding androidx.databinding.DataBinderMapper.getDataBinder(androidx.databindi