Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

skydoves/BaseRecyclerViewAdapter

Repository files navigation

LicenseAPIBuild StatusJavadoc
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.

demo0demo1

Including in your project

Maven CentralJitPack

Gradle

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"}

Usage

  1. Create a custom ViewHolder class extendingBaseViewHolder by your custom layout.
    bindData method receives an item model what "Any" type in Kotlin or "object" type in Java.
    and you can implementonClickItem listener 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}
  1. Create a custom Adapter class extendingBaseAdapter.
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)    }}
  1. 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)    }}

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.

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")    }}

Multi-Type Sections

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

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.

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 -> {        }    }}

License

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

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp