FastAdapter ada di sini untuk menyederhanakan pembuatan adaptor untuk Recyclerviews. Jangan khawatir tentang adaptor lagi. Cukup tulis logika untuk tampilan/item Anda seharusnya, dan Anda sudah selesai. Ini menyala cepat, meminimalkan kode yang perlu Anda tulis, dan mudah diperluas.
Apa yang disertakan • Pengaturan • Panduan Migrasi? • Digunakan oleh • Contoh Aplikasi
Perpustakaan dibagi menjadi inti, commons, dan ekstensi. Fungsi inti termasuk dalam ketergantungan berikut.
implementation " com.mikepenz:fastadapter: ${ latestFastAdapterRelease } "
implementation " androidx.appcompat:appcompat: ${ androidX } "
implementation " androidx.recyclerview:recyclerview: ${ androidX } "Dukungan yang dapat diperluas disertakan dan dapat ditambahkan melalui ini
implementation " com.mikepenz:fastadapter-extensions-expandable: ${ latestFastAdapterRelease } "Banyak kelas pembantu termasuk dalam ketergantungan berikut.
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 } " Cukup buat kelas yang memperluas AbstractItem seperti yang ditunjukkan di bawah ini. Terapkan metode, dan item Anda sudah siap.
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 )
}
} Gunakan ketergantungan ekstensi binding dalam aplikasi Anda untuk ini.
// 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 ) Secara default FastAdapter hanya menyediakan fungsionalitas dasar, yang dilengkapi dengan abstraksi item sebagai Item dan Model . Dan fungsionalitas umum penambahan/menghapus/memodifikasi elemen. Untuk mengaktifkan pilihan , atau memperluas ekstensi yang disediakan perlu diaktifkan.
// 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. Ini membutuhkan ekstensi
fastadapter-extensions-expandable.
// Gets (or creates and attaches if not yet existing) the extension.
val expandableExtension = fastAdapter.getExpandableExtension()
// configure as needed
expandableExtension.isOnlyOneExpandedItem = true Untuk perincian lebih lanjut, gulir ke bawah ke bagian ExpandableItems (di bawah penggunaan lanjutan).
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() harus mengembalikan true untuk item yang akan dipertahankan dan salah untuk item yang akan dihapus.
Ini membutuhkan ekstensi
fastadapter-extensions-drag.
Pertama, lampirkan ItemTouchHelper ke RecyclerView.
val dragCallback = SimpleDragCallback ()
val touchHelper = ItemTouchHelper (dragCallback)
touchHelper.attachToRecyclerView(recyclerView) Menerapkan antarmuka ItemTouchCallback dalam aktivitas Anda, dan mengganti metode itemTouchOnMove() .
override fun itemTouchOnMove ( oldPosition : Int , newPosition : Int ): Boolean {
DragDropUtil .onMove(fastItemAdapter.itemAdapter, oldPosition, newPosition) // change position
return true
}Mulailah dengan menginisialisasi adaptor Anda:
// Header is a model class for your header
val headerAdapter = ItemAdapter < Header >()Inisialisasi model fastAdapter:
val itemAdapter = GenericItemAdapter ()Akhirnya, atur adaptor:
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) Buat footerAdapter. Kami membutuhkan ini untuk menampilkan ProgressBar pemuatan di akhir daftar kami. (Jangan lupa untuk meneruskannya ke FastAdapter.with(..) )
val footerAdapter = ItemAdapter < ProgressItem >()Perlu diingat bahwa ProgressItem disediakan oleh ekstensi 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 )
}
})Untuk tutorial lengkap dan lebih banyak fitur seperti multi-select dan CAB, lihat aplikasi sampel.
FastAdapter dengan proguard FastAdapter hadir dengan dukungan untuk barang -barang yang dapat diperluas. Setelah menambahkan ketergantungan, atur ekstensi Expandable melalui:
val expandableExtension = fastAdapter.getExpandableExtension() Item yang dapat diperluas harus mengimplementasikan antarmuka IExpandable , dan sub item antarmuka ISubItem . Ini memungkinkan dukungan yang lebih baik. Aplikasi sampel menyediakan implementasi sampel dari itu. (Yang ada dalam sampel disimpan model yang memungkinkan mereka digunakan dengan orang tua / subitem yang berbeda)
Pada cara bagaimana SubItems dan keadaan mereka ditangani, sangat disarankan untuk menggunakan StateManagement berbasis identifier . Cukup tambahkan withPositionBasedStateManagement(false) ke pengaturan FastAdapter Anda.
Item sederhana hanya perlu diperpanjang dari AbstractExpandableItem dan memberikan jenis ViewHolder .
open class SimpleSubExpandableItem : AbstractExpandableItem < SimpleSubExpandableItem . ViewHolder >() {
/* *
* BASIC ITEM IMPLEMENTATION
*/
} // Lihat SimpleSubExpandableItem.kt dari aplikasi sampel untuk detail lebih lanjut.
Mike Penz:
Mike Penz
Fabian Terhorst
Perangkat lunak open source gratis ini juga dimungkinkan oleh sekelompok sukarelawan yang menempatkan banyak jam kerja keras ke dalamnya. Lihat file kontributor.md untuk detailnya.
Terima kasih khusus kepada kontributor yang sangat aktif yang menambahkan banyak peningkatan ke perpustakaan ini.
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.