FastAdapter在這裡簡化了為回收即可創建適配器。不再擔心適配器。只需編寫有關您的視圖/項目的外觀的邏輯,就可以完成。它正在快速燃燒,最小化您需要編寫的代碼,並且易於擴展。
包括什麼包括•設置•遷移指南? ••示例應用程序使用
庫被分為核心,公共和擴展。核心功能包括在以下依賴性中。
implementation " com.mikepenz:fastadapter: ${ latestFastAdapterRelease } "
implementation " androidx.appcompat:appcompat: ${ androidX } "
implementation " androidx.recyclerview:recyclerview: ${ androidX } "包括可擴展的支持,可以通過此添加
implementation " com.mikepenz:fastadapter-extensions-expandable: ${ latestFastAdapterRelease } "以下依賴性包括許多輔助類別。
implementation " com.mikepenz:fastadapter-extensions-binding: ${ latestFastAdapterRelease } " // view binding helpers
implementation " com.mikepenz:fastadapter-extensions-diff: ${ latestFastAdapterRelease } " // diff util helpers
implementation " com.mikepenz:fastadapter-extensions-drag: ${ latestFastAdapterRelease } " // drag support
implementation " com.mikepenz:fastadapter-extensions-paged: ${ latestFastAdapterRelease } " // paging support
implementation " com.mikepenz:fastadapter-extensions-scroll: ${ latestFastAdapterRelease } " // scroll helpers
implementation " com.mikepenz:fastadapter-extensions-swipe: ${ latestFastAdapterRelease } " // swipe support
implementation " com.mikepenz:fastadapter-extensions-ui: ${ latestFastAdapterRelease } " // pre-defined ui components
implementation " com.mikepenz:fastadapter-extensions-utils: ${ latestFastAdapterRelease } " // needs the `expandable`, `drag` and `scroll` extension.
// required for the ui components and the utils
implementation " com.google.android.material:material: ${ androidX } " 只需創建一個延長AbstractItem的類,如下所示。實施方法,您的項目已經準備就緒。
open class SimpleItem : AbstractItem < SimpleItem . ViewHolder >() {
var name : String? = null
var description : String? = null
/* * defines the type defining this item. must be unique. preferably an id */
override val type : Int
get() = R .id.fastadapter_sample_item_id
/* * defines the layout which will be used for this item in the list */
override val layoutRes : Int
get() = R .layout.sample_item
override fun getViewHolder ( v : View ): ViewHolder {
return ViewHolder (v)
}
class ViewHolder ( view : View ) : FastAdapter.ViewHolder<SimpleItem>(view) {
var name : TextView = view.findViewById( R .id.material_drawer_name)
var description : TextView = view.findViewById( R .id.material_drawer_description)
override fun bindView ( item : SimpleItem , payloads : List < Any >) {
name.text = item.name
description.text = item.name
}
override fun unbindView ( item : SimpleItem ) {
name.text = null
description.text = null
}
}
}
class BindingIconItem : AbstractBindingItem < IconItemBinding >() {
var name : String? = null
override val type : Int
get() = R .id.fastadapter_icon_item_id
override fun bindView ( binding : IconItemBinding , payloads : List < Any >) {
binding.name.text = name
}
override fun createBinding ( inflater : LayoutInflater , parent : ViewGroup ? ): IconItemBinding {
return IconItemBinding .inflate(inflater, parent, false )
}
}為此,請在應用程序中使用binding擴展依賴項。
// create the ItemAdapter holding your Items
val itemAdapter = ItemAdapter < SimpleItem >()
// create the managing FastAdapter, by passing in the itemAdapter
val fastAdapter = FastAdapter . with (itemAdapter)
// set our adapters to the RecyclerView
recyclerView.setAdapter(fastAdapter)
// set the items to your ItemAdapter
itemAdapter.add( ITEMS )默認情況下, FastAdapter僅提供基本功能,該功能由Item和Model的抽象帶來。以及添加/刪除/修改元素的一般功能。要啟用選擇或擴展物,需要激活提供的擴展。
// Gets (or creates and attaches if not yet existing) the extension from the given `FastAdapter`
val selectExtension = fastAdapter.getSelectExtension()
// configure as needed
selectExtension.isSelectable = true
selectExtension.multiSelect = true
selectExtension.selectOnLongClick = false
// see the API of this class for more options. 這需要
fastadapter-extensions-expandableExtension。
// Gets (or creates and attaches if not yet existing) the extension.
val expandableExtension = fastAdapter.getExpandableExtension()
// configure as needed
expandableExtension.isOnlyOneExpandedItem = true有關更多詳細信息,請向下滾動到ExpandableItems (在高級用法下)部分。
fastAdapter.onClickListener = { view, adapter, item, position ->
// Handle click here
false
} // just add an `EventHook` to your `FastAdapter` by implementing either a `ClickEventHook`, `LongClickEventHook`, `TouchEventHook`, `CustomEventHook`
fastAdapter.addEventHook( object : ClickEventHook < SimpleImageItem >() {
override fun onBind ( viewHolder : RecyclerView . ViewHolder ): View ? {
// return the views on which you want to bind this event
return if (viewHolder is SimpleImageItem . ViewHolder ) {
viewHolder.viewWhichReactsOnClick
} else {
null
}
}
override fun onClick ( v : View , position : Int , fastAdapter : FastAdapter < SimpleImageItem >, item : SimpleImageItem ) {
// react on the click event
}
}) // Call this in onQueryTextSubmit() & onQueryTextChange() when using SearchView
itemAdapter.filter( " yourSearchTerm " )
itemAdapter.itemFilter.filterPredicate = { item : SimpleItem , constraint : CharSequence? ->
item.name?.text.toString().contains(constraint.toString(), ignoreCase = true )
} filter()應返回true,以保留要保留的項目,並為要刪除的項目false。
這需要
fastadapter-extensions-drag擴展。
首先,將ItemTouchHelper附加到回收庫。
val dragCallback = SimpleDragCallback ()
val touchHelper = ItemTouchHelper (dragCallback)
touchHelper.attachToRecyclerView(recyclerView)在您的活動中實現ItemTouchCallback接口,並覆蓋itemTouchOnMove()方法。
override fun itemTouchOnMove ( oldPosition : Int , newPosition : Int ): Boolean {
DragDropUtil .onMove(fastItemAdapter.itemAdapter, oldPosition, newPosition) // change position
return true
}首先初始化您的適配器:
// Header is a model class for your header
val headerAdapter = ItemAdapter < Header >()初始化模型FastAdapter:
val itemAdapter = GenericItemAdapter ()最後,設置適配器:
val fastAdapter : GenericFastAdapter = FastAdapter . with (headerAdapter, itemAdapter) // the order defines in which order the items will show up
// alternative the super type of both item adapters can be used. e.g.:
recyclerView.setAdapter(fastAdapter)創建一個footeradapter。我們需要在列表的末尾顯示加載進度鍵。 (別忘了將其傳遞到FastAdapter.with(..) )
val footerAdapter = ItemAdapter < ProgressItem >()請記住,ProgressItem由FastAdapter的擴展名提供。
recyclerView.addOnScrollListener( object : EndlessRecyclerOnScrollListener (footerAdapter) {
override fun onLoadMore ( currentPage : Int ) {
footerAdapter.clear()
footerAdapter.add( ProgressItem ())
// Load your items here and add it to FastAdapter
itemAdapter.add( NEWITEMS )
}
})有關完整的教程以及更多功能,例如多选和CAB,請查看示例應用程序。
FastAdapter v2.5.0開始 FastAdapter提供了對可擴展項目的支持。添加依賴關係後,通過以下方式設置了Expandable擴展。
val expandableExtension = fastAdapter.getExpandableExtension()可擴展的項目必須實現IExpandable接口,以及ISubItem接口的子項目。這可以更好地支持。示例應用程序提供了這些樣本實現。 (樣本中的那些保留模型,允許它們與不同的父 /子立場一起使用)
截至SubItems及其狀態如何處理方式,強烈建議使用基於identifier的StateManagement 。只需在您的FastAdapter設置中添加withPositionBasedStateManagement(false)即可。
一個簡單的項目只需要從AbstractExpandableItem擴展,並將ViewHolder作為類型提供。
open class SimpleSubExpandableItem : AbstractExpandableItem < SimpleSubExpandableItem . ViewHolder >() {
/* *
* BASIC ITEM IMPLEMENTATION
*/
} //有關更多詳細信息,請參見示例應用程序的SimpleSubExpandableItem.kt 。
邁克·彭茲(Mike Penz):
邁克·彭茲(Mike Penz)
Fabian Terhorst
一群志願者也使這種免費的開源軟件成為可能。有關詳細信息,請參見contrutors.md文件。
特別感謝非常活躍的貢獻者,他們為該庫增加了許多改進。
Copyright 2021 Mike Penz
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.