Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork15
⚡ Fast way to bind RecyclerView adapter and ViewHolder for implementing clean sections.
License
skydoves/BaseRecyclerViewAdapter
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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.
Add below codes to yourrootbuild.gradle file (not your module build.gradle file).
allprojects { repositories { mavenCentral() }}And add a dependency code to yourmodule'sbuild.gradle file.
dependencies { implementation"com.github.skydoves:baserecyclerviewadapter:1.0.4"}
- Create a custom ViewHolder class extending
BaseViewHolderby your custom layout.bindDatamethod receives an item model what "Any" type in Kotlin or "object" type in Java.
and you can implementonClickItemlistener about the item or whatever.
classSampleViewHolder(view:View,privatevaldelegate:Delegate) :BaseViewHolder(view) {privatelateinitvarsampleItem:SampleIteminterfaceDelegate {funonItemClick(sampleItem:SampleItem) }overridefunbindData(data:Any) {if(dataisSampleItem) {sampleItem =datadrawItem() } }privatefundrawItem() {itemView.run {sample0_avatar.image =sampleItem.imagesample0_name.text =sampleItem.namesample0_content.text =sampleItem.content } }overridefunonClick(v:View?) {delegate.onItemClick(this.sampleItem) }overridefunonLongClick(v:View?) =false}
- Create a custom Adapter class extending
BaseAdapter.
classSampleAdapter0(privatevaldelegate:SampleViewHolder.Delegate) :BaseAdapter() {privatevalsection_item =0init {addSection(ArrayList<SampleItem>()) }funaddItems(sampleItems:List<SampleItem>) {addItemsOnSection(section_item,sampleItems)notifyDataSetChanged() }overridefunlayout(sectionRow:SectionRow):Int {returnR.layout.item_sample }overridefunviewHolder(layout:Int,view:View):BaseViewHolder {returnSampleViewHolder(view,delegate) }}
- And use at Activities or Fragments.
classSampleActivity0 :AppCompatActivity(),SampleViewHolder.Delegate {privatevaladapterbylazy {SampleAdapter0(this) }overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_sample0)sample0_recyclerView.adapter =adaptersample0_recyclerView.layoutManager =LinearLayoutManager(this)mockItems() }privatefunmockItems() {adapter.addItems(MockSamples.mockSampleItems(this,15)) }overridefunonItemClick(sampleItem:SampleItem) {toast(sampleItem.name) }}
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.
classSampleAdapter1(privatevaldelegate:SampleViewHolder.Delegate):BaseAdapter() {init {for(iin0..5) {addSection(ArrayList<Any>()) } }funaddItems(section:Int,items:List<SampleItem>) {addItemOnSection(section,"Section$section")addItemsOnSection(section,items)notifyDataSetChanged() }overridefunlayout(sectionRow:SectionRow):Int {when(sectionRow.row()) {0 ->returnR.layout.item_sample1_headerelse ->returnR.layout.item_sample } }overridefunviewHolder(layout:Int,view:View):BaseViewHolder {when(layout) {R.layout.item_sample1_header ->returnSampleViewHolder1_Header(view)R.layout.item_sample ->returnSampleViewHolder(view,delegate) }throwResources.NotFoundException("not founded layout") }}
Or you can handle multi-layout by sections like below.
classGithubUserAdapter(valdelegate_header:GithubUserHeaderViewHolder.Delegate,valdelegate:GithubUserViewHolder.Delegate) :BaseAdapter() {privatevalsection_header =0privatevalsection_follower =1init {addSection(ArrayList<GithubUser>())addSection(ArrayList<Follower>()) }funupdateHeader(resource:Resource<GithubUser>) {resource.data?.let {sections[section_header].clear()sections[section_header].add(it)notifyDataSetChanged() } }funaddFollowList(followers:List<Follower>) {sections[section_follower].addAll(followers)notifyDataSetChanged() }overridefunlayout(sectionRow:BaseAdapter.SectionRow):Int { when (sectionRow.section()) {section_header ->returnR.layout.layout_detail_headerelse ->returnR.layout.item_github_user } }overridefunviewHolder(layout:Int,view:View):BaseViewHolder { when (layout) {R.layout.layout_detail_header ->returnGithubUserHeaderViewHolder(view,delegate_header)else ->returnGithubUserViewHolder(view,delegate) } }
RecyclerViewPaginator lets you implementation paging and endless-recyclerView easily.
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.
privatevaladapterbylazy {SampleAdapter0(this) }privatelateinitvarpaginator:RecyclerViewPaginatoroverridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_sample2)sample2_recyclerView.adapter =adaptersample2_recyclerView.layoutManager =LinearLayoutManager(this)paginator =RecyclerViewPaginator(recyclerView =sample2_recyclerView,onLast = {false },loadMore = {loadMore() },isLoading = {false } )loadMore()}privatefunloadMore() {adapter.addItems(MockSamples.mockSampleItemsRandom(this,paginator.currentPage *10,10))}
This is anexample of RecyclerViewPaginator with ViewModel's network fetching.
And you can reference more at thisrepository.
overridefunonCreate(savedInstanceState:Bundle?) {AndroidInjection.inject(this)super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)main_recyclerView.adapter =adaptermain_recyclerView.layoutManager =LinearLayoutManager(this)paginator =RecyclerViewPaginator(recyclerView =main_recyclerView,isLoading = {viewModel.fetchStatus.isOnLoading },loadMore = {loadMore(it) },onLast = {viewModel.fetchStatus.isOnLast } )initializeUI()observeViewModel()}privatefunloadMore(page:Int) {viewModel.postPage(page)}privatefunupdateGithubUser(resource:Resource<GithubUser>) {when (resource.status) {Status.SUCCESS ->adapter.updateHeader(resource)Status.ERROR ->toast(resource.message.toString())Status.LOADING -> { } }}
Copyright 2018 skydovesLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
About
⚡ Fast way to bind RecyclerView adapter and ViewHolder for implementing clean sections.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.

