RecyclerView+Databinding最佳实践(一)

RecyclerView+Databing最佳实践 一

  • 效果图
  • 添加DataBinding支持
  • 代码部分
    • 文件目录
    • 代码部分
      • text_item.xml
      • activity_main.xml
      • MainActivity.kt
      • mBindingAdapter.kt
      • mBindingAdapterItem.kt
      • Text.kt

效果图

RecyclerView+Databinding最佳实践(一)_第1张图片

添加DataBinding支持

添加

ext.gradle_version = '4.0.2'//gradle版本

注意红色框中的版本号要一致
同时,绿色框中的内容是生成binding类必须的
RecyclerView+Databinding最佳实践(一)_第2张图片

添加

apply plugin: 'kotlin-kapt'//kapt 插件

 //开启dataBinding
    dataBinding {
        enabled = true
    }

RecyclerView+Databinding最佳实践(一)_第3张图片

代码部分

文件目录

RecyclerView+Databinding最佳实践(一)_第4张图片

代码部分

text_item.xml


<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="item"
            type="com.example.databindingtestpractice.Text" />
    data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@{item.imageID}" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{item.text}"
            android:gravity="center"
            android:textSize="25sp"/>
    LinearLayout>
layout>

activity_main.xml


<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:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"/>

layout>

MainActivity.kt

package com.example.databindingtestpractice

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.databindingtestpractice.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
     
    override fun onCreate(savedInstanceState: Bundle?) {
     
        super.onCreate(savedInstanceState)
        val items: MutableList<mBindingAdapterItem> = ArrayList()
        items.apply {
     
            add(Text("哈哈哈哈",R.drawable.device))
            add(Text("我又来啦",R.drawable.device))
            add(Text("我还来",R.drawable.device))
            add(Text("就是不让你看美女",R.drawable.device))
            add(Text("哈哈你当不住我看见啦",R.drawable.device))
        }
        val binding:ActivityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
        val mmBindingAdapter:mBindingAdapter = mBindingAdapter(items)
        val manager: LinearLayoutManager = LinearLayoutManager(applicationContext)
        binding.recyclerView.layoutManager = manager
        binding.recyclerView.adapter = mmBindingAdapter
        mmBindingAdapter.notifyDataSetChanged()
    }
}

mBindingAdapter.kt

package com.example.databindingtestpractice

import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder


class mBindingAdapter(var items: MutableList<mBindingAdapterItem>) : RecyclerView.Adapter<mBindingAdapter.BindingHolder>() {
     

    companion object{
     
        @BindingAdapter("android:src")
        @JvmStatic
        fun setSrc(view: ImageView, bitmap: Bitmap?) {
     
            view.setImageBitmap(bitmap)
        }

        @BindingAdapter("android:src")
        @JvmStatic
        fun setSrc(view: ImageView, resId: Int) {
     
            view.setImageResource(resId)
        }
    }

    /**
     * @return 返回的是adapter的view
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
     
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            viewType,
            parent,
            false
        )
        return BindingHolder(binding)
    }

    /*
    * 数据绑定
    * */
    override fun onBindViewHolder(holder: BindingHolder, position: Int) {
     
        holder.bindData(items[position])
    }

    override fun getItemCount() = items.size

    override fun getItemViewType(position: Int) = items[position].getViewType()

    class BindingHolder(var binding: ViewDataBinding) : ViewHolder(binding.root) {
     
        fun bindData(item: mBindingAdapterItem?) {
     
            binding.setVariable(BR.item, item)
        }
    }
}

mBindingAdapterItem.kt

类对象通过继承这个接口来告诉Adapter其对应的Item布局
例如Text对象的返回值是R.layout.text_item,而Image对象的返回值是R.layout.image_item,他们都继承了mBindingAdapterItem,因而都可以存放在适配器数据源数组MutableList< mBindingAdapterItem >

package com.example.databindingtestpractice

interface mBindingAdapterItem {
     
    fun getViewType(): Int
}

Text.kt

package com.example.databindingtestpractice

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable


class Text(val text: String,val ImageID:Int):BaseObservable(),mBindingAdapterItem{
     
    override fun getViewType(): Int {
     
        return R.layout.text_item
    }

    @JvmName("getText1")
    @Bindable
    fun getText(): String? {
     
        return text
    }

    @JvmName("getImageID1")
    @Bindable
    fun getImageID(): Int? {
     
        return ImageID
    }
}

你可能感兴趣的:(Kotlin,android,kotlin)