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

时间:2018-04-09 09:52:43

标签: android-recyclerview kotlin adapter

我写了一个Kotlin FirebaseRecyclerAdapter,它可以很好地作为我的MainActivity的一部分。但是,我想将此代码放在一个单独的MainAdapter.kt文件/类中。我怎么能这样做?

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
        }
    }
}

1 个答案:

答案 0 :(得分:0)

鉴于您的代码,您可以执行以下操作:

.modal-backdrop.in {
  opacity: 0.9;
}

.modal-backdrop.in + .modal-backdrop.in {
  opacity: 0.3;
}

还有很多其他方法可以解决这个问题,这只是你的封装版本。