如何将此 Firebase Recycler Adapter 实现转换为 Adapter.kt class?

How can I convert this Firebase Recycler Adapter implementation into an Adapter.kt class?

我编写了一个 Kotlin FirebaseRecyclerAdapter,作为我的 MainActivity 的一部分工作得很好。但是,我想将此代码放在单独的 MainAdapter.kt file/class 中。我该怎么做?

var query = FirebaseDatabase.getInstance()
        .reference
        .child("").child("categories")
        .limitToLast(50)

val options = FirebaseRecyclerOptions.Builder<Category>()
        .setQuery(query, Category::class.java)
        .setLifecycleOwner(this)
        .build()

val adapter = object : FirebaseRecyclerAdapter<Category, CategoryHolder>(options) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.category_row, parent, false))
    }

    protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
        holder.bind(model)
    }

    override fun onDataChanged() {
        // Called each time there is a new data snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }
}

class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {

    fun bind(category: Category) {
        with(category) {
            customView.textView_name?.text = category.name
            customView.textView_description?.text = category.description
        }
    }
}

根据您的代码,您可以这样做:

class MainAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Category, CategoryHolder>(buildOptions(lifecycleOwner)) {

    companion object {
        private fun buildQuery() = FirebaseDatabase.getInstance()
                .reference
                .child("").child("categories")
                .limitToLast(50)

        private fun buildOptions(lifecycleOwner:LifecycleOwner) = FirebaseRecyclerOptions.Builder<Category>()
                .setQuery(buildQuery(), Category::class.java)
                .setLifecycleOwner(lifecycleOwner)
                .build()

    }

    class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {

        fun bind(category: Category) {
            with(category) {
                customView.textView_name?.text = category.name
                customView.textView_description?.text = category.description
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.category_row, parent, false))
    }

    protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
        holder.bind(model)
    }

    override fun onDataChanged() {
        // Called each time there is a new data snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }
}

这个问题还有很多其他的处理方法,这里只是你的一个封装版本。