基于kotlin的,DataBinding+Recycleview,并实现item的子控件的点击事件

基于kotlin的,DataBinding+Recycleview,并实现item的子控件的点击事件

核心代码如下:Activity

class MainActivity : AppCompatActivity() {
    val studentInfo: Student? = null
    lateinit var binding: ActivityMainBinding
    lateinit var recyclerView: RecyclerView
    var users: MutableList = mutableListOf()
    lateinit var user: User
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//        binding.user = User("Test","User")
//        val binding: ActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater())
        val studentInfo = Student()
        studentInfo.name.set("我叫朝阳杨大爷")
        studentInfo.age.set(20)
        binding.studentInfo = studentInfo
        binding.eventListener = EventListener()
        recyclerView = binding.rvContent
        recyclerView.layoutManager = LinearLayoutManager(this)

        for (i in 0 until 30) {
            user = User(null,null)
            user.firstName = "我是$i"
            user.lastName = "他是$i"
            users.add(user)
//            User("我是$i", "他是$i")
        }
        val adapter = users?.let { FruitAdapter(it, BR.user) }
        recyclerView.adapter = adapter
//        binding.setClick { toast("点击了我") }
//        binding.tvStudenname.setOnClickListener { studentInfo.name.set("我还是叫叫朝阳杨大爷") }
//        binding.tvAge.setOnClickListener {  studentInfo.age.set(18)}
//        val map = ObservableArrayMap()
//        map["name"]= "我叫杨大爷"
//        map["age"] = 20
//        binding.studentInfo = map


    }

    inner class EventListener {

        fun onClick(view: View) {
            binding.studentInfo!!.name.set("我叫不是你大爷")
            binding.studentInfo!!.age.set(200)
//            studentInfo.age.set(200)
            Toast.makeText(this@MainActivity, "点击了并刷新worker对象的值", Toast.LENGTH_LONG).show()
        }

        fun btnClick(view: View) {
            view.id
            Toast.makeText(this@MainActivity, "监听器方式", Toast.LENGTH_LONG).show()
        }
//         fun onTextChanged(charSequence :CharSequence ,  i:Int, i1:Int , i2:Int ){
//             binding.studentInfo!!.age.set(300)
//
//         }

        fun onLongClick(view: View): Boolean {
            Toast.makeText(this@MainActivity, "长按了", Toast.LENGTH_LONG).show()
            return false
        }
    }

}

Adapter:

class FruitAdapter (itemList: List, brId: Int) : BaseAdapter(itemList, brId) {
    var onItemClickListener: AdapterView.OnItemClickListener? = null

    override fun getLayoutId(): Int {
        return R.layout.fruit_item //item对应的layoutId
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.binding.tvAges.setOnClickListener { mContext!!.toast("我被点了,我是第${position}个") }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return super.onCreateViewHolder(parent, viewType)

    }

}

BaseAdapter:

//两个泛型,T代表item的model类型即itemList的bean类型,TBinding代表itemLayout对应生成的XXXBinding类,比如itemLayout是fruit_item.xml,对应的就是FruitItemBinding
//两个参数itemList对应具体要显示的arrayList,brId则对应itemLayout里相应variable的变量名,比如variable的name是fruit,brId就是BR.fruit
abstract class BaseAdapter(private val itemList: List,
                                                         private val brId: Int) : RecyclerView.Adapter.ViewHolder>() {

    protected var mContext: Context? = null

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {//相当于itemView
        lateinit var binding: TBinding
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (mContext == null) {
            mContext = parent.context
        }
        //因为TBinding的具体类型不确定,这里只能用DataBindingUtil.inflate(),而不能用XXXBinding.inflate()
        val binding: TBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), parent, false)
        val holder = ViewHolder(binding.root)
        holder.binding = binding
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.setVariable(brId, itemList[position])
        //立即执行绑定,在对view变化时效敏感的地方常用,不加这句有可能出现itemView更新滞后、闪烁等问题
        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int {
        return itemList.size
    }
    //获取具体子adapter对应的itemLayoutId
    abstract fun getLayoutId(): Int

}

Activity对象的Xml:



    
        
        
        
        
        
        
        
        
        
        
    
    
        

Adapter对应的xml:



    

    

    
    




源码如下:

https://download.csdn.net/download/u013075460/12484252

 

你可能感兴趣的:(小技巧)