【问题标题】:RecyclerView Adapter + Data BindingRecyclerView 适配器 + 数据绑定
【发布时间】:2020-06-05 07:00:28
【问题描述】:

好的,所以我正在尝试在我的 recyclerview 适配器中实现数据绑定,我需要帮助,因为我不知道具体如何?我正在尝试从我的 recyclerview 适配器中删除样板代码,这就是原因。在下面检查我的代码:

custom_row(Recyclerview 项目布局)

<?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>
        <variable
            name="toDoData"
            type="com.jovanovic.stefan.tododemo.data.ToDoData" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/row_background"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:background="@drawable/item_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/title_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="16dp"
                android:text="@{toDoData.title}"
                android:textColor="@color/darkGray"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/description_txt"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:maxLength="160"
                android:maxLines="3"
                android:text="@{toDoData.description}"
                android:textColor="@color/darkGray"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/title_txt"
                app:layout_constraintTop_toBottomOf="@+id/title_txt" />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

ToDoData

@Parcelize
@Entity(tableName = "todo_table")
data class ToDoData(
    @PrimaryKey(autoGenerate = true)
    var id: Int,
    var title: String,
    var priority: Int,
    var description: String
) : Parcelable

MyAdapter(Recyclerview 适配器)

class MyAdapter: RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
       val view = LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        TODO("Not yet implemented")
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        TODO("Not yet implemented")
    }

}

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

    你快到了。要完成您的实施,请执行以下操作。

    在您的适配器中,创建将支持数据绑定的视图持有者。

    class ViewHolder private constructor(private val binding: CustomRowBinding)
            : RecyclerView.ViewHolder(binding.root) {
    
            fun bind(todo: ToDoData) {
                binding.todo = toDoData
                // make sure to include this so your view will be updated
                binding.executePendingBindings()
            }
    
            companion object {
                fun from(parent: ViewGroup): ViewHolder {
                    val layoutInflater = LayoutInflater.from(parent.context)
                    val binding = CustomRowBinding.inflate(layoutInflater, parent, false)
    
                    return ViewHolder(binding)
                }
            }
        }
    

    在你的 oncreateViewHolder 中

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

    最后是 onBindViewHolder

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val todo = todoList[position] // this will be the list object you created
            holder.bind(todo)
        }
    

    【讨论】:

    • 我已经删除了我的“MyViewHolder”,并用您提供的那个替换了它。它有效,谢谢! :)
    • “CustomRowBinding”类是如何生成的?请解释一下我正在尝试但找不到我的 recycleView 绑定类。
    【解决方案2】:

    你可以试试SmartRecyclerView

    smartRecyclerView = findViewById(R.id.smartRecyclerView)
    smartRecyclerView.apply{
            initSmartRecyclerView(activity = this,smartRecyclerViewListener = smartRecyclerViewListener,isPaginated = true)
            isEnabled = false // enable/disable SwipeRefreshLayout
            setClickListener(onItemClickListener) // (optional, set 
            clickListener on recyclerview items)
            setViewAttachListener(viewAttachListener) // (optional, set viewAttachListener on recyclerview items)
            setScrollListener(recyclerViewListener) // (optional, set 
            setScrollListener on recyclerview items)
            setShimmerLayout(R.layout.item_loader) // (optional, set shimmer layout while user waits for the data to load)
         }
    
    
    
    
    private val smartRecyclerViewListener:SmartRecyclerViewListener<T> = object:SmartRecyclerViewListener<T>{
    
            override fun getItemViewType(model: T): Int {
                return 0 //return viewType from model
            }
    
            override fun getViewLayout(viewType: Int): Int {
                return R.layout.item_file // on the basis of viewType return the layout you want for the recyclerview item.
            }
    
            override fun setListSize(size: Int) {
                //this method will be called whenever smartRecyclerView undergoes any operation.
            }
    
            override fun onRefresh() {
                //do something on refresh....
                smartRecyclerView.isRefreshing = false
                Toast.makeText(baseContext,"OnRefresh Called",Toast.LENGTH_LONG).show()
            }
    
            override fun onLoadNext() {
            // onLoadNext() will be called if isPaginated = true and user scrolls to bottom or the smartRecyclerView.
                Toast.makeText(baseContext,"OnLoadNext",Toast.LENGTH_LONG).show()
            }
        
        
            //DiffUtils Callback functions
        
            override fun areContentsTheSame(newItem: T, oldItem: T): Boolean {
                return newItem.distance==oldItem.distance
            }
    
            override fun areItemsTheSame(newItem: T, oldItem: T): Boolean {
                return newItem.uuid==oldItem.uuid
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 2017-12-15
      • 1970-01-01
      • 2016-03-05
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多