Skip to content

Pidsamhai/pinnedrv

Repository files navigation

Pinned Recycler View

   GitHub

GetStart

Jitpack

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  • Gradle
implementation 'com.github.Pidsamhai:pinnedrv:<latest-version>'
  • DSL
implementation("com.github.Pidsamhai:pinnedrv:<latest-version>")

Example

  • MainActivity
class MainActivity : AppCompatActivity() {

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

        val dataSet = getMockData()

        val adapter = RecycleViewAdapter()
        recycleView.pinnedAdapter = adapter
        adapter.items.addAll(dataSet)
        adapter.notifyDataSetChanged()

        PinnedTabMediator(
            tabLayout,
            recycleView,
            { tab, position ->
                tab.text = (dataSet[position] as Category).categoryName
            }
        ).attach()

    }
}
  • Adapter
class RecycleViewAdapter : PinnedRvAdapter<RecycleViewAdapter.VH>() {

    class VH(v: View): RecyclerView.ViewHolder(v)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        return when (viewType) {
            HEADER -> VH(
                LayoutInflater.from(parent.context).inflate(R.layout.header, parent, false)
            )
            else -> VH(
                LayoutInflater.from(parent.context).inflate(R.layout.product, parent, false)
            )
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: VH, position: Int) {
        when (val item = items[position]) {
            is Header -> {
                (item as Category)
                holder.itemView.header.text = item.categoryName
            }
            is Child -> {
                holder.itemView.apply {
                    (item as Product)
                    name.text = item.name
                    price.text = "$ %,.2f".format(item.price)
                }
            }
        }
    }
}
  • implement class Header and Child
data class Category(
    val categoryId: Int,
    val categoryName: String
) : Header()

data class Product(
    val productId: Int,
    val name: String,
    val cover: String,
    val price: Float
): Child()

License

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Copyright (C) 2020 Pidsamhai <meng348@gmail.com>

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.

About

Android Lib, Help sync Tablayout and RecyclerView

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages