baserecyclerviewadapter

Fast way to bind RecyclerView adapter and ViewHolder for implementing clean sections.

License

License

GroupId

GroupId

com.github.skydoves
ArtifactId

ArtifactId

baserecyclerviewadapter
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

aar
Description

Description

baserecyclerviewadapter
Fast way to bind RecyclerView adapter and ViewHolder for implementing clean sections.
Project URL

Project URL

https://github.com/skydoves/BaseRecyclerViewAdapter/
Source Code Management

Source Code Management

https://github.com/skydoves/BaseRecyclerViewAdapter/

Download baserecyclerviewadapter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.skydoves/baserecyclerviewadapter/ -->
<dependency>
    <groupId>com.github.skydoves</groupId>
    <artifactId>baserecyclerviewadapter</artifactId>
    <version>1.0.4</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.skydoves/baserecyclerviewadapter/
implementation 'com.github.skydoves:baserecyclerviewadapter:1.0.4'
// https://jarcasting.com/artifacts/com.github.skydoves/baserecyclerviewadapter/
implementation ("com.github.skydoves:baserecyclerviewadapter:1.0.4")
'com.github.skydoves:baserecyclerviewadapter:aar:1.0.4'
<dependency org="com.github.skydoves" name="baserecyclerviewadapter" rev="1.0.4">
  <artifact name="baserecyclerviewadapter" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.skydoves', module='baserecyclerviewadapter', version='1.0.4')
)
libraryDependencies += "com.github.skydoves" % "baserecyclerviewadapter" % "1.0.4"
[com.github.skydoves/baserecyclerviewadapter "1.0.4"]

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.20

runtime (1)

Group / Artifact Type Version
androidx.recyclerview » recyclerview jar 1.1.0

Project Modules

There are no modules declared in this project.

BaseRecyclerViewAdapter

License API Build Status Javadoc
BaseAdapter and BaseViewHolder for binding each other fastly and implementing multi-sectioned rows easily.
And lets you implement paging and endless-scrolling for RecyclerView easily.

demo0 demo1

Including in your project

Maven Central JitPack

Gradle

Add below codes to your root build.gradle file (not your module build.gradle file).

allprojects {
    repositories {
        mavenCentral()
    }
}

And add a dependency code to your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:baserecyclerviewadapter:1.0.4"
}

Usage

  1. Create a custom ViewHolder class extending BaseViewHolder by your custom layout.
    bindData method receives an item model what "Any" type in Kotlin or "object" type in Java.
    and you can implement onClickItem listener about the item or whatever.
class SampleViewHolder(view: View, private val delegate: Delegate) : BaseViewHolder(view) {

    private lateinit var sampleItem: SampleItem

    interface Delegate {
        fun onItemClick(sampleItem: SampleItem)
    }

    override fun bindData(data: Any) {
        if(data is SampleItem) {
            sampleItem = data
            drawItem()
        }
    }

    private fun drawItem() {
        itemView.run {
            sample0_avatar.image = sampleItem.image
            sample0_name.text = sampleItem.name
            sample0_content.text = sampleItem.content
        }
    }

    override fun onClick(v: View?) {
        delegate.onItemClick(this.sampleItem)
    }

    override fun onLongClick(v: View?) = false
}
  1. Create a custom Adapter class extending BaseAdapter.
class SampleAdapter0(private val delegate: SampleViewHolder.Delegate) : BaseAdapter() {

    private val section_item = 0

    init {
        addSection(ArrayList<SampleItem>())
    }

    fun addItems(sampleItems: List<SampleItem>) {
        addItemsOnSection(section_item, sampleItems)
        notifyDataSetChanged()
    }

    override fun layout(sectionRow: SectionRow): Int {
        return R.layout.item_sample
    }

    override fun viewHolder(layout: Int, view: View): BaseViewHolder {
        return SampleViewHolder(view, delegate)
    }
}
  1. And use at Activities or Fragments.
class SampleActivity0 : AppCompatActivity(), SampleViewHolder.Delegate {

    private val adapter by lazy { SampleAdapter0(this) }

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

        sample0_recyclerView.adapter = adapter
        sample0_recyclerView.layoutManager = LinearLayoutManager(this)
        mockItems()
    }

    private fun mockItems() {
        adapter.addItems(MockSamples.mockSampleItems(this, 15))
    }

    override fun onItemClick(sampleItem: SampleItem) {
        toast(sampleItem.name)
    }
}

Multi-Type Rows

If you want to implement multi-sections or rows on a RecyclerView, you should create more than two custom ViewHolders.
And you can handle multi-layout like below.

class SampleAdapter1(private val delegate: SampleViewHolder.Delegate): BaseAdapter() {

    init {
        for(i in 0..5) {
            addSection(ArrayList<Any>())
        }
    }

    fun addItems(section: Int, items: List<SampleItem>) {
        addItemOnSection(section, "Section$section")
        addItemsOnSection(section, items)
        notifyDataSetChanged()
    }

    override fun layout(sectionRow: SectionRow): Int {
        when(sectionRow.row()) {
            0 -> return R.layout.item_sample1_header
            else -> return R.layout.item_sample
        }
    }

    override fun viewHolder(layout: Int, view: View): BaseViewHolder {
        when(layout) {
            R.layout.item_sample1_header -> return SampleViewHolder1_Header(view)
            R.layout.item_sample -> return SampleViewHolder(view, delegate)
        }
        throw Resources.NotFoundException("not founded layout")
    }
}

Multi-Type Sections

Or you can handle multi-layout by sections like below.

class GithubUserAdapter(val delegate_header: GithubUserHeaderViewHolder.Delegate,
                        val delegate: GithubUserViewHolder.Delegate) : BaseAdapter() {

    private val section_header = 0
    private val section_follower = 1

    init {
        addSection(ArrayList<GithubUser>())
        addSection(ArrayList<Follower>())
    }

    fun updateHeader(resource: Resource<GithubUser>) {
        resource.data?.let {
            sections[section_header].clear()
            sections[section_header].add(it)
            notifyDataSetChanged()
        }
    }

    fun addFollowList(followers: List<Follower>) {
        sections[section_follower].addAll(followers)
        notifyDataSetChanged()
    }

    override fun layout(sectionRow: BaseAdapter.SectionRow): Int {
        when (sectionRow.section()) {
            section_header -> return R.layout.layout_detail_header
            else -> return R.layout.item_github_user
        }
    }

    override fun viewHolder(layout: Int, view: View): BaseViewHolder {
        when (layout) {
            R.layout.layout_detail_header -> return GithubUserHeaderViewHolder(view, delegate_header)
            else -> return GithubUserViewHolder(view, delegate)
        }
    }

RecyclerViewPaginator

RecyclerViewPaginator lets you implementation paging and endless-recyclerView easily.

demo2

RecylcerViewPaginator performs invoke loadMore when recyclerView needs to load more items.
And it would not be called when fetching from network or loading ended.
This is an example of endless-recyclerView.

private val adapter by lazy { SampleAdapter0(this) }
private lateinit var paginator: RecyclerViewPaginator

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

    sample2_recyclerView.adapter = adapter
    sample2_recyclerView.layoutManager = LinearLayoutManager(this)
    paginator = RecyclerViewPaginator(
            recyclerView = sample2_recyclerView,
            onLast = { false },
            loadMore = { loadMore() },
            isLoading = { false }
    )
    loadMore()
}

private fun loadMore() {
    adapter.addItems(MockSamples.mockSampleItemsRandom(this, paginator.currentPage * 10, 10))
}

This is an example of RecyclerViewPaginator with ViewModel's network fetching.
And you can reference more at this repository.

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

    main_recyclerView.adapter = adapter
    main_recyclerView.layoutManager = LinearLayoutManager(this)
    paginator = RecyclerViewPaginator(
            recyclerView = main_recyclerView,
            isLoading = { viewModel.fetchStatus.isOnLoading },
            loadMore = { loadMore(it) },
            onLast = { viewModel.fetchStatus.isOnLast }
    )

    initializeUI()
    observeViewModel()
}

private fun loadMore(page: Int) {
    viewModel.postPage(page)
}

private fun updateGithubUser(resource: Resource<GithubUser>) {
    when (resource.status) {
        Status.SUCCESS -> adapter.updateHeader(resource)
        Status.ERROR -> toast(resource.message.toString())
        Status.LOADING -> {
        }
    }
}

License

Copyright 2018 skydoves

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.

Versions

Version
1.0.4