Awesome Open Source
Awesome Open Source

MoreType

new method to build data in RecyclerView with Kotlin!

download

Click icon download lastest sample

English |

Build Status Hex.pm Download API

**Keyword: Data driven view **

Preview

more-type-one more-type-two

Dependency

compile 'com.werb.moretype:moretype:$last_version'

or

implementation 'com.werb.moretype:moretype:$last_version'

Update log

v0.4.0

  • bug fix
  • update MoreViewHolder Constructor

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(values: MutableMap<String, Any>, 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.


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Kotlin (59,757
Android (41,017
Kotlin Android (3,093
Recyclerview (1,343
Kotlin Library (766
Recyclerview Adapter (368
Related Projects