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.