Android jetpack DataBinding 与RecyclerView
生活随笔
收集整理的這篇文章主要介紹了
Android jetpack DataBinding 与RecyclerView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.anguomob.jecpack.beandata class Idol2(val chName: String, val enName: String, val image: String)
父布局
<?xml version="1.0" encoding="utf-8"?> <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"><data></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".activity.RecycerViewActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout> </layout>父代碼
package com.anguomob.jecpack.activityimport androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.LinearLayoutManager import com.anguomob.jecpack.R import com.anguomob.jecpack.adapter.RecyclerViewAdapter import com.anguomob.jecpack.bean.Idol2 import com.anguomob.jecpack.databinding.ActivityRecycerViewBindingclass RecycerViewActivity : AppCompatActivity() {private lateinit var binding: ActivityRecycerViewBindingoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = DataBindingUtil.setContentView(this, R.layout.activity_recycer_view)binding.rv.layoutManager = LinearLayoutManager(this)val adapter = RecyclerViewAdapter()binding.rv.adapter = adapteradapter.setData(getListData())}private fun getListData(): List<Idol2> {var data = mutableListOf<Idol2>()data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))data.add(Idol2("二狗","two dog","https://t7.baidu.com/it/u=3676218341,3686214618&fm=193&f=GIF"))data.add(Idol2("小姐姐","xiao jie jie","https://img0.baidu.com/it/u=962361882,2281204904&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"))return data} }adapter 代碼
package com.anguomob.jecpack.adapterimport android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.RecyclerView import com.anguomob.jecpack.R import com.anguomob.jecpack.bean.Idol2 import com.anguomob.jecpack.databinding.ItemBindingclass RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewAdapter.RVViewHolder>() {var mData: MutableList<Idol2> = mutableListOf();lateinit var itemBinding: ItemBindingfun setData(data: List<Idol2>) {this.mData.addAll(data)notifyDataSetChanged()}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVViewHolder {itemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),R.layout.item,parent,false)return RVViewHolder(itemBinding);}override fun onBindViewHolder(holder: RVViewHolder, position: Int) {val data = mData.get(position);holder.itemBinding.idol = data;}override fun getItemCount(): Int {return mData.size}class RVViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {lateinit var itemBinding: ItemBindingconstructor(itemBinding: ItemBinding) : this(itemBinding.root) {this.itemBinding = itemBinding;}} }adapter 布局
<?xml version="1.0" encoding="utf-8"?> <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"><data><variablename="idol"type="com.anguomob.jecpack.bean.Idol2" /></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"tools:context=".activity.DatabindingActivity"><ImageViewandroid:id="@+id/imageView"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginStart="28dp"android:src="@mipmap/ic_launcher"app:image="@{idol.image}"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="36dp"android:text="@{idol.chName}"android:textSize="24dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.471"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:text="姓名" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginBottom="20dp"android:text="@{idol.enName}"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView1"tools:text="年齡" /></androidx.constraintlayout.widget.ConstraintLayout> </layout>這里有一個image屬性
直接跳轉到這里
Kotlin jetpack BindingAdapter 簡單的給布局添加自定義屬性_安果移不動的博客-CSDN博客
哦對 可以使用
tools:listitem="@layout/item"對布局進行預覽
?
總結
以上是生活随笔為你收集整理的Android jetpack DataBinding 与RecyclerView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: angularjs+chosen的使用备
- 下一篇: TS类型辨析