FastAdapter อยู่ที่นี่เพื่อลดความซับซ้อนของการสร้างอะแดปเตอร์สำหรับ Recyclerviews ไม่ต้องกังวลกับอะแดปเตอร์อีกต่อไป เพียงแค่เขียนตรรกะสำหรับวิธีการดู/รายการของคุณควรมีลักษณะและคุณทำเสร็จแล้ว มันดูเร็วที่สุดลดรหัสที่คุณต้องเขียนและขยายง่าย
มีอะไรรวม•คู่มือการตั้งค่า•คู่มือการโยกย้าย? •ใช้โดย•แอปตัวอย่าง
ห้องสมุดถูกแบ่งออกเป็นแกนกลางและส่วนขยาย ฟังก์ชั่นหลักจะรวมอยู่ในการพึ่งพาต่อไปนี้
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-expandable
// 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() ควรส่งคืนจริงสำหรับรายการที่จะเก็บไว้และเท็จสำหรับรายการที่จะลบออก
สิ่งนี้ต้องใช้ส่วนขยาย
fastadapter-extensions-drag
ก่อนอื่นให้แนบ ItemTouchHelper เพื่อ Recyclerview
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 เราต้องการสิ่งนี้เพื่อแสดงการโหลด ProgressBar ในตอนท้ายของรายการของเรา (อย่าลืมที่จะส่งผ่านไปยัง 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 )
}
})สำหรับการสอนที่สมบูรณ์และคุณสมบัติอื่น ๆ เช่นการเลือกแบบหลายตัวเลือกและห้องโดยสารตรวจสอบแอพตัวอย่าง
FastAdapter กับ proguard FastAdapter มาพร้อมกับการสนับสนุนสำหรับรายการที่ขยายได้ หลังจากเพิ่มการตั้งค่าการตั้งค่าส่วนขยาย Expandable ผ่าน:
val expandableExtension = fastAdapter.getExpandableExtension() รายการที่ขยายได้จะต้องใช้อินเทอร์เฟซ IExpandable และรายการย่อยของอินเตอร์เฟส ISubItem สิ่งนี้ช่วยให้การสนับสนุนดีขึ้น แอพตัวอย่างให้การใช้งานตัวอย่างของสิ่งเหล่านั้น (ผู้ที่อยู่ในตัวอย่างจะถูกเก็บไว้ซึ่งช่วยให้พวกเขาใช้กับผู้ปกครอง / subitems ที่แตกต่างกัน)
เป็นวิธีที่วิธีการจัดการ SubItems และสถานะของพวกเขาขอแนะนำอย่างยิ่งให้ใช้ StateManagement ตาม identifier เพียงเพิ่ม withPositionBasedStateManagement(false) ลงในการตั้งค่า FastAdapter ของคุณ
รายการง่าย ๆ เพียงแค่ต้องขยายจาก AbstractExpandableItem และให้ ViewHolder เป็นประเภท
open class SimpleSubExpandableItem : AbstractExpandableItem < SimpleSubExpandableItem . ViewHolder >() {
/* *
* BASIC ITEM IMPLEMENTATION
*/
} // ดู SimpleSubExpandableItem.kt ของแอปพลิเคชันตัวอย่างสำหรับรายละเอียดเพิ่มเติม
Mike Penz:
ไมค์เพนซ์
Fabian Terhorst
ซอฟต์แวร์โอเพ่นซอร์สฟรีนี้ก็เป็นไปได้โดยกลุ่มอาสาสมัครที่ใช้เวลาหลายชั่วโมงในการทำงานอย่างหนัก ดูรายละเอียดไฟล์ผู้ร่วมให้ข้อมูล
ขอขอบคุณเป็นพิเศษสำหรับผู้มีส่วนร่วมที่ใช้งานมากซึ่งเพิ่มการปรับปรุงมากมายให้กับห้องสมุดนี้
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.