Giter Site home page Giter Site logo

shaohuancode / multitype Goto Github PK

View Code? Open in Web Editor NEW

This project forked from drakeet/multitype

0.0 0.0 0.0 8.26 MB

An Android library makes it easier and more flexible to create multiple types of RecyclerViews.

License: Apache License 2.0

Kotlin 100.00%

multitype's Introduction

MultiType

An Android library makes it easier and more flexible to create multiple types of RecyclerViews.

Build Status License maven-central jetbrains-plugin

Previously, when we need to develop a complex RecyclerView / ListView, it is difficult and troublesome work. We should override the getItemViewType() of RecyclerView.Adapter , add some types, and create some ViewHolders relating to those types. Once we need to add a new item type, we have to go to the original adapter file and modify some old codes carefully, and these adapter classes will get more complicated.

Nowadays, I created a new intuitive and flexible way to easily create complex RecyclerViews, with the MultiType library, we could insert a new item type without changing any old adapter codes and make them more readable.

Getting started

In your build.gradle:

MultiType has been rebuilt based on AndroidX. If you are still using the android support library, please use multitype:3.4.4 and multitype-kotlin:3.4.4.

In addition, since 4.0.0 we have migrated to fully build with Kotlin. If you don't want to use Kotlin, you can use the last stable version multitype:3.5.0 and see 3.x.

dependencies {
  implementation 'me.drakeet.multitype:multitype:4.0.0-alpha3'
}

Usage

Step 1. Create a Kotlin class or data class, for example:

data class Foo(
  val value: String
)

Step 2. Create a class extends ItemViewBinder<T, VH : ViewHolder>, for example:

class FooViewBinder: ItemViewBinder<Foo, FooViewBinder.ViewHolder>() {

  override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
    return ViewHolder(inflater.inflate(R.layout.item_foo, parent, false))
  }

  override fun onBindViewHolder(holder: ViewHolder, item: Foo) {
    holder.fooView.text = item.value
    Log.d("ItemViewBinder API", "position: ${getPosition(holder)}")
    Log.d("ItemViewBinder API", "items: $adapterItems")
    Log.d("ItemViewBinder API", "adapter: $adapter")
    Log.d("More", "Context: ${holder.itemView.context}")
  }

  class ViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
    val fooView: TextView = itemView.findViewById(R.id.foo)
  }
}

Step 3. register your types and setup your RecyclerView, for example:

class SampleActivity : AppCompatActivity() {

  private lateinit var adapter: MultiTypeAdapter
  private lateinit var items: MutableList<Any>

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_list)
    val recyclerView = findViewById<RecyclerView>(R.id.list)

    adapter = MultiTypeAdapter()
    adapter.register(TextItemViewBinder())
    adapter.register(ImageItemViewBinder())
    adapter.register(RichItemViewBinder())
    recyclerView.adapter = adapter

    val textItem = TextItem("world")
    val imageItem = ImageItem(R.mipmap.ic_launcher)
    val richItem = RichItem("小艾大人赛高", R.drawable.img_11)

    items = ArrayList()
    for (i in 0..19) {
      items.add(textItem)
      items.add(imageItem)
      items.add(richItem)
    }
    adapter.items = items
    adapter.notifyDataSetChanged()
  }
}

That's all, you're good to go!

Advanced usage

One to many:

adapter.register(Data::class).to(
  DataType1ViewBinder(),
  DataType2ViewBinder()
).withKotlinClassLinker { _, data ->
  when (data.type) {
    Data.TYPE_2 -> DataType2ViewBinder::class
    else -> DataType1ViewBinder::class
  }
}

See OneDataToManyActivity, OneToManyFlow and OneToManyEndpoint for more details.

More methods that you can override from ItemViewBinder:

open fun onBindViewHolder(holder: VH, item: T, payloads: List<Any>)
open fun getItemId(item: T): Long
open fun onViewRecycled(holder: VH)
open fun onFailedToRecycleView(holder: VH): Boolean
open fun onViewAttachedToWindow(holder: VH)
open fun onViewDetachedFromWindow(holder: VH)

Android Studio Plugin

An intellij idea plugin for Android to generate MultiType Item and ItemViewBinder easily.

Screenshots

Pages created with MultiType:

Wiki

License

Copyright (c) 2016-present. Drakeet Xu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

multitype's People

Contributors

drakeet avatar hamberluo avatar shawnlinboy avatar yeungkc avatar kxfeng avatar feicien avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.