/ RecyclerView

New method to build data in RecyclerView with Kotlin

New method to build data in RecyclerView with Kotlin

MoreType

new method to build data in RecyclerView with Kotlin!

type2

Dependency

compile 'com.werb.moretype:moretype:0.2.5'

or

implementation 'com.werb.moretype:moretype:0.2.5'

Update log

v0.2.5

  • fix fix payloads judge error
  • update support lib version

Usage

Keyword: Data driven view 【数据驱动视图】

Step 1. create a data model class, like:

data class SingleText(val title: String, val desc: String, val url: String)

or

class SingleText {
    var title: String? = null
    var desc: String? = null
    var url: String? = null
}

Step 2. create a class (xxxViewHolder) extends abstract class MoreViewHolder<T : Any>() , like:

// Register layoutID here or Register with adapter in Activity
@LayoutID(R.layout.item_view_single_type_one)
class SingleTypeOneViewHolder(containerView: View) : MoreViewHolder<SingleText>(containerView) {

    override fun bindData(data: SingleText, payloads: List<Any>) {
        title.text = data.title
        desc.text = data.desc
        icon.setImageURI(data.url)
    }

}

Step 3. register and attach to recyclerview in Any where you build list, like:

import kotlinx.android.synthetic.main.activity_single_register.*

class SingleRegisterActivity: AppCompatActivity() {

    private val adapter = MoreAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_single_register)

        list.layoutManager = LinearLayoutManager(this)

        /* register ViewHolder and attach to recyclerView */
        adapter.apply {
            // two method register Layout
            register(RegisterItem(R.layout.item_view_single_type_one, SingleTypeOneViewHolder::class.java))
            // or
            register(SingleTypeOneViewHolder::class.java)
            attachTo(single_register_list)
        }

        /* load any data List or model object */
        adapter.loadData(DataServer.getSingleRegisterData())

    }

}

Upon completion of these three steps, a list based on the [Data Driven View] has been completed.

GitHub