### BaseMultiItemAdapter Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/BaseMultiItemAdapter.md Example demonstrating how to implement and use BaseMultiItemAdapter with different item types. ```APIDOC ## Usage Example ### Define Data Model ```kotlin sealed class ItemData { data class HeaderItem(val title: String) : ItemData() data class ContentItem(val text: String) : ItemData() data class FooterItem(val info: String) : ItemData() } ``` ### Implement MultiAdapter ```kotlin class MultiAdapter : BaseMultiItemAdapter() { companion object { const val TYPE_HEADER = 1 const val TYPE_CONTENT = 2 const val TYPE_FOOTER = 3 } init { // Register handler for header type addItemType(TYPE_HEADER, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): HeaderVH { return HeaderVH(parent.getItemView(R.layout.item_header)) } override fun onBind(holder: HeaderVH, position: Int, item: ItemData?) { if (item is ItemData.HeaderItem) { holder.bind(item) } } }) // Register handler for content type addItemType(TYPE_CONTENT, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ContentVH { return ContentVH(parent.getItemView(R.layout.item_content)) } override fun onBind(holder: ContentVH, position: Int, item: ItemData?) { if (item is ItemData.ContentItem) { holder.bind(item) } } }) // Register handler for footer type addItemType(TYPE_FOOTER, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): FooterVH { return FooterVH(parent.getItemView(R.layout.item_footer)) } override fun onBind(holder: FooterVH, position: Int, item: ItemData?) { if (item is ItemData.FooterItem) { holder.bind(item) } } }) // Set viewType determination logic onItemViewType { position, list -> when (list[position]) { is ItemData.HeaderItem -> TYPE_HEADER is ItemData.ContentItem -> TYPE_CONTENT is ItemData.FooterItem -> TYPE_FOOTER } } } // ViewHolder classes inner class HeaderVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.HeaderItem) { itemView.findViewById(R.id.title).text = item.title } } inner class ContentVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.ContentItem) { itemView.findViewById(R.id.text).text = item.text } } inner class FooterVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.FooterItem) { itemView.findViewById(R.id.info).text = item.info } } } ``` ### Usage ```kotlin val adapter = MultiAdapter() val items = listOf( ItemData.HeaderItem("Section 1"), ItemData.ContentItem("Item 1"), ItemData.ContentItem("Item 2"), ItemData.FooterItem("End of section") ) adapter.submitList(items) recyclerView.adapter = adapter ``` ``` -------------------------------- ### DetailAdapter Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/BaseSingleItemAdapter.md An example of how to implement and use BaseSingleItemAdapter. It shows creating a custom adapter, setting up the RecyclerView, and updating the item with and without payloads. ```kotlin class DetailAdapter : BaseSingleItemAdapter() { override fun onCreateViewHolder( context: Context, parent: ViewGroup, viewType: Int ): VH { return VH(parent.getItemView(R.layout.item_detail)) } override fun onBindViewHolder(holder: VH, item: DetailData?) { holder.bind(item) } inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: DetailData?) { // Bind detail data itemView.findViewById(R.id.title).text = item?.title } } } // Usage val adapter = DetailAdapter() recyclerView.adapter = adapter // Set item adapter.item = DetailData(title = "Hello") // Update with payload adapter.setItem(newData, "partial_update_marker") ``` -------------------------------- ### Basic Usage Example with Preset Animations Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/ItemAnimator.md Demonstrates how to enable and apply preset animations to a RecyclerView adapter. ```kotlin // Use preset animations via AnimationType enum val adapter = MyAdapter() // Enable animations adapter.animationEnable = true // Animate only on first load adapter.isAnimationFirstOnly = true // Apply preset animation adapter.setItemAnimation(BaseQuickAdapter.AnimationType.ScaleIn) // Or set directly adapter.itemAnimation = AlphaInAnimation(duration = 500) adapter.itemAnimation = SlideInLeftAnimation(duration = 300) // Customize animation parameters adapter.itemAnimation = AlphaInAnimation( duration = 600, mFrom = 0.5f ) ``` -------------------------------- ### CustomLeadingAdapter Implementation Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LeadingLoadStateAdapter.md Example of creating a custom LeadingLoadStateAdapter to define the UI for the loading state. ```APIDOC ## CustomLeadingAdapter ### Description This class extends `LeadingLoadStateAdapter` to provide a custom view holder and binding logic for the loading state. ### Class Definition ```kotlin class CustomLeadingAdapter : LeadingLoadStateAdapter() { override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadingVH { // Inflate the layout for the loading item return LoadingVH(parent.getItemView(R.layout.item_loading)) } override fun onBindViewHolder(holder: LoadingVH, loadState: LoadState) { // Control the visibility of loading UI based on the load state when (loadState) { is LoadState.Loading -> holder.showLoading() else -> holder.hide() } } inner class LoadingVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun showLoading() { // Show the progress bar itemView.findViewById(R.id.progress).visibility = View.VISIBLE } fun hide() { // Hide the entire item view itemView.visibility = View.GONE } } } ``` ``` -------------------------------- ### Basic Setup for Leading Load More Source: https://github.com/cymchad/baserecyclerviewadapterhelper/wiki/首部加载更多(向上加载更多) Initialize the adapter and QuickAdapterHelper with a default leading load state adapter. The RecyclerView should use the adapter provided by the helper. ```kotlin val mAdapter = RecyclerViewAdapter() val helper = QuickAdapterHelper.Builder(mAdapter) .setLeadingLoadStateAdapter(object : OnLeadingListener { override fun onLoad() { request() } override fun isAllowLoading(): Boolean { return true } }).build() recyclerView.adapter = helper.adapter ``` -------------------------------- ### BaseQuickAdapter Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/BaseQuickAdapter.md Demonstrates the creation and usage of a custom BaseQuickAdapter with DiffUtil, item click listeners, child view click listeners, animations, and state view configuration. ```kotlin // Define adapter with DiffUtil callback val diffCallback = object : DiffUtil.ItemCallback() { override fun areItemsTheSame(old: Item, new: Item) = old.id == new.id override fun areContentsTheSame(old: Item, new: Item) = old == new } class MyAdapter : BaseQuickAdapter(diffCallback) { override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(parent.getItemView(R.layout.item_layout)) } override fun onBindViewHolder(holder: MyViewHolder, position: Int, item: Item?) { holder.bind(item) } } // Usage val adapter = MyAdapter() // Submit data adapter.submitList(itemList) // Add items adapter.add(newItem) adapter.addAll(listOf(item1, item2)) // Set listeners adapter.setOnItemClickListener { adapter, view, position -> Toast.makeText(view.context, "Clicked: ${adapter.getItem(position)}", Toast.LENGTH_SHORT).show() } adapter.addOnItemChildClickListener(R.id.button) { adapter, view, position -> Log.d("Click", "Child view clicked at $position") } // Set animation adapter.setItemAnimation(BaseQuickAdapter.AnimationType.ScaleIn) // Use state layout adapter.isStateViewEnable = true adapter.setStateViewLayout(context, R.layout.layout_empty_state) ``` -------------------------------- ### QuickAdapterHelper Setup and Usage Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickAdapterHelper.md This snippet illustrates the complete process of setting up QuickAdapterHelper, including defining the main content adapter, configuring leading and trailing load state adapters with their respective listeners and properties, building the helper, attaching the adapter to a RecyclerView, and updating various load states like loading, error, and end of pagination. ```kotlin val contentAdapter = MyItemAdapter() val leadingAdapter = DefaultLeadingLoadStateAdapter().apply { isLoadEnable = true preloadSize = 5 setOnLeadingListener(object : LeadingLoadStateAdapter.OnLeadingListener { override fun onLoad() { loadPreviousPage() } override fun isAllowLoading(): Boolean { return !isRefreshing } }) } val trailingAdapter = DefaultTrailingLoadStateAdapter().apply { isAutoLoadMore = true preloadSize = 3 setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener { override fun onLoad() { loadNextPage() } override fun onFailRetry() { loadNextPage() } override fun isAllowLoading(): Boolean { return !isRefreshing } }) } val helper = QuickAdapterHelper.Builder(contentAdapter) .setLeadingLoadStateAdapter(leadingAdapter) .setTrailingLoadStateAdapter(trailingAdapter) .build() recyclerView.adapter = helper.adapter contentAdapter.submitList(itemList) helper.leadingLoadState = LoadState.Loading helper.trailingLoadState = LoadState.NotLoading(endOfPaginationReached = false) helper.trailingLoadState = LoadState.Error(exception) helper.trailingLoadState = LoadState.NotLoading(endOfPaginationReached = true) ``` -------------------------------- ### Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/ItemAnimator.md Demonstrates how to enable and apply animations to a RecyclerView adapter, including using preset animations or custom implementations. ```APIDOC ## Usage Example ```kotlin // Use preset animations via AnimationType enum val adapter = MyAdapter() // Enable animations adapter.animationEnable = true // Animate only on first load adapter.isAnimationFirstOnly = true // Apply preset animation adapter.setItemAnimation(BaseQuickAdapter.AnimationType.ScaleIn) // Or set directly adapter.itemAnimation = AlphaInAnimation(duration = 500) adapter.itemAnimation = SlideInLeftAnimation(duration = 300) // Customize animation parameters adapter.itemAnimation = AlphaInAnimation( duration = 600, mFrom = 0.5f ) ``` ``` -------------------------------- ### Custom LeadingLoadStateAdapter Implementation Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LeadingLoadStateAdapter.md Implement a custom adapter to control the display of loading states at the start of a RecyclerView. This example shows how to create a custom adapter, define view holders, and handle different load states. ```kotlin class CustomLeadingAdapter : LeadingLoadStateAdapter() { override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadingVH { return LoadingVH(parent.getItemView(R.layout.item_loading)) } override fun onBindViewHolder(holder: LoadingVH, loadState: LoadState) { when (loadState) { is LoadState.Loading -> holder.showLoading() else -> holder.hide() } } inner class LoadingVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun showLoading() { itemView.findViewById(R.id.progress).visibility = View.VISIBLE } fun hide() { itemView.visibility = View.GONE } } } ``` -------------------------------- ### LoadState Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LoadState.md Demonstrates how to check and react to different LoadState values in your application logic. ```APIDOC ## Usage Example ```kotlin // Check load state when (loadState) { is LoadState.None -> { // Initial or refresh state showPlaceholder() } is LoadState.Loading -> { // Show loading indicator showLoadingSpinner() } is LoadState.NotLoading -> { // Not loading hideLoadingSpinner() if (loadState.endOfPaginationReached) { // All data loaded showEndMessage() } } is LoadState.Error -> { // Show error showErrorMessage(loadState.error.message) showRetryButton() } } // Check if at end if (loadState.endOfPaginationReached) { disableLoadMore() } // Check if loading if (loadState is LoadState.Loading) { stopSwipeRefresh() } ``` ``` -------------------------------- ### QuickAdapterHelper Composition Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/INDEX.md QuickAdapterHelper facilitates combining multiple adapters, including load state adapters, using ConcatAdapter. This is ideal for implementing pagination and mixed content views. ```kotlin val concatAdapter = ConcatAdapter( LeadingLoadStateAdapter(), contentAdapter, TrailingLoadStateAdapter() ) recyclerView.adapter = QuickAdapterHelper.with(concatAdapter) ``` -------------------------------- ### Custom Loading State Adapter Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LoadStateAdapter.md Demonstrates creating a custom LoadStateAdapter to display different loading, error, and completion states. It shows how to bind view holders and control the display of load states. This custom adapter is then used with QuickAdapterHelper. ```kotlin // Create custom loading state adapter class CustomLoadingAdapter : LoadStateAdapter() { override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadingVH { return LoadingVH(parent.getItemView(R.layout.item_loading)) } override fun onBindViewHolder(holder: LoadingVH, loadState: LoadState) { when (loadState) { is LoadState.Loading -> { holder.showLoading() } is LoadState.Error -> { holder.showError(loadState.error.message) } is LoadState.NotLoading -> { if (loadState.endOfPaginationReached) { holder.showComplete() } else { holder.hide() } } is LoadState.None -> holder.hide() } } override fun displayLoadStateAsItem(loadState: LoadState): Boolean { // Show Loading and Error, hide others return super.displayLoadStateAsItem(loadState) } inner class LoadingVH(itemView: View) : RecyclerView.ViewHolder(itemView) { private val spinner = itemView.findViewById(R.id.spinner) private val error = itemView.findViewById(R.id.error_msg) private val complete = itemView.findViewById(R.id.complete_msg) fun showLoading() { spinner.visibility = View.VISIBLE error.visibility = View.GONE complete.visibility = View.GONE } fun showError(message: String?) { spinner.visibility = View.GONE error.visibility = View.VISIBLE error.text = message ?: "Error loading data" complete.visibility = View.GONE } fun showComplete() { spinner.visibility = View.GONE error.visibility = View.GONE complete.visibility = View.VISIBLE } fun hide() { spinner.visibility = View.GONE error.visibility = View.GONE complete.visibility = View.GONE } } } // Use with QuickAdapterHelper val trailingAdapter = CustomLoadingAdapter() val helper = QuickAdapterHelper.Builder(contentAdapter) .setTrailingLoadStateAdapter(trailingAdapter) .build() recyclerView.adapter = helper.adapter // Listen for state changes trailingAdapter.addLoadStateListener { previous, current -> Log.d("LoadState", "Changed from $previous to $current") } // Update state when loading trailingAdapter.loadState = LoadState.Loading trailingAdapter.loadState = LoadState.NotLoading(endOfPaginationReached = false) // Handle error trailingAdapter.loadState = LoadState.Error(exception) ``` -------------------------------- ### Start Drag by ViewHolder Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickDragAndSwipe.md Programmatically initiates a drag operation for a specific ViewHolder. ```kotlin open fun startDrag(holder: RecyclerView.ViewHolder): QuickDragAndSwipe ``` -------------------------------- ### Start Swipe by ViewHolder Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickDragAndSwipe.md Programmatically initiates a swipe operation for a specific ViewHolder. ```kotlin open fun startSwipe(holder: RecyclerView.ViewHolder): QuickDragAndSwipe ``` -------------------------------- ### Start Swipe by Position Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickDragAndSwipe.md Programmatically initiates a swipe operation for the item at the specified position. ```kotlin open fun startSwipe(position: Int): QuickDragAndSwipe ``` -------------------------------- ### Start Drag by Position Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickDragAndSwipe.md Programmatically initiates a drag operation for the item at the specified position. ```kotlin open fun startDrag(position: Int): QuickDragAndSwipe ``` -------------------------------- ### TrailingLoadStateAdapter Usage Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/TrailingLoadStateAdapter.md Example demonstrating how to create a custom TrailingLoadStateAdapter, configure its properties like preloadSize and isAutoLoadMore, and set the OnTrailingListener. ```APIDOC ## TrailingLoadStateAdapter Configuration ### Description This section details how to implement and integrate the `TrailingLoadStateAdapter` into your RecyclerView. It covers creating a custom adapter, setting up listeners, and integrating with `QuickAdapterHelper`. ### Key Properties - **`preloadSize`** (Int): The number of items from the bottom of the list that triggers the load more action. - **`isAutoLoadMore`** (Boolean): If true, automatically triggers `onLoadMore` when `preloadSize` is reached. ### Setting the Load Listener ```kotlin val trailingAdapter = CustomTrailingAdapter().apply { // Auto-load when 3 items from bottom preloadSize = 3 isAutoLoadMore = true // Set load listener setOnLoadMoreListener(object : OnTrailingListener { override fun onLoad() { // Fetch next page from API viewModel.loadNextPage() } override fun onFailRetry() { // Retry failed request viewModel.retryLoadNextPage() } override fun isAllowLoading(): Boolean { // Don't load while refreshing return !isRefreshing } }) } ``` ### Integration with QuickAdapterHelper ```kotlin // Integrate with QuickAdapterHelper val helper = QuickAdapterHelper.Builder(contentAdapter) .setTrailingLoadStateAdapter(trailingAdapter) .build() recyclerView.adapter = helper.adapter ``` ### Managing Load State ```kotlin // Use in ViewModel/Activity fun loadPage() { // Show loading trailingAdapter.loadState = LoadState.Loading // Fetch data... // On success if (page == lastPage) { trailingAdapter.loadState = LoadState.NotLoading(endOfPaginationReached = true) } else { trailingAdapter.loadState = LoadState.NotLoading(endOfPaginationReached = false) } // On error trailingAdapter.loadState = LoadState.Error(exception) } // Handle not-full-page scenario fun submitPartialPage(items: List) { contentAdapter.submitList(currentList + items) // Disable load more if content not full screen trailingAdapter.checkDisableLoadMoreIfNotFullPage() } ``` ``` -------------------------------- ### BaseMultiItemAdapter Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/BaseMultiItemAdapter.md Demonstrates how to implement and use BaseMultiItemAdapter with multiple view types, including data model definition, item type registration, and view holder implementation. ```kotlin // Define data model with type sealed class ItemData { data class HeaderItem(val title: String) : ItemData() data class ContentItem(val text: String) : ItemData() data class FooterItem(val info: String) : ItemData() } class MultiAdapter : BaseMultiItemAdapter() { companion object { const val TYPE_HEADER = 1 const val TYPE_CONTENT = 2 const val TYPE_FOOTER = 3 } init { // Register handler for header type addItemType(TYPE_HEADER, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): HeaderVH { return HeaderVH(parent.getItemView(R.layout.item_header)) } override fun onBind(holder: HeaderVH, position: Int, item: ItemData?) { if (item is ItemData.HeaderItem) { holder.bind(item) } } }) // Register handler for content type addItemType(TYPE_CONTENT, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ContentVH { return ContentVH(parent.getItemView(R.layout.item_content)) } override fun onBind(holder: ContentVH, position: Int, item: ItemData?) { if (item is ItemData.ContentItem) { holder.bind(item) } } }) // Register handler for footer type addItemType(TYPE_FOOTER, object : OnMultiItem() { override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): FooterVH { return FooterVH(parent.getItemView(R.layout.item_footer)) } override fun onBind(holder: FooterVH, position: Int, item: ItemData?) { if (item is ItemData.FooterItem) { holder.bind(item) } } }) // Set viewType determination logic onItemViewType { position, list -> when (list[position]) { is ItemData.HeaderItem -> TYPE_HEADER is ItemData.ContentItem -> TYPE_CONTENT is ItemData.FooterItem -> TYPE_FOOTER } } } // ViewHolder classes inner class HeaderVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.HeaderItem) { itemView.findViewById(R.id.title).text = item.title } } inner class ContentVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.ContentItem) { itemView.findViewById(R.id.text).text = item.text } } inner class FooterVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(item: ItemData.FooterItem) { itemView.findViewById(R.id.info).text = item.info } } } // Usage val adapter = MultiAdapter() val items = listOf( ItemData.HeaderItem("Section 1"), ItemData.ContentItem("Item 1"), ItemData.ContentItem("Item 2"), ItemData.FooterItem("End of section") ) adapter.submitList(items) recyclerView.adapter = adapter ``` -------------------------------- ### Custom Implementation Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/ItemAnimator.md Guides on creating custom `ItemAnimator` implementations, such as rotation or complex combined animations, and applying them. ```APIDOC ## Custom Implementation ```kotlin // Implement custom animation class RotateInAnimation(private val duration: Long = 300) : ItemAnimator { override fun animator(view: View): Animator { return ObjectAnimator.ofFloat(view, "rotation", 0f, 360f).apply { this.duration = this@RotateInAnimation.duration interpolator = AccelerateDecelerateInterpolator() } } } // Use custom animation adapter.itemAnimation = RotateInAnimation(duration = 500) // Or combine multiple animations with AnimatorSet class ComplexAnimation : ItemAnimator { override fun animator(view: View): Animator { return AnimatorSet().apply { val alpha = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f) val scale = ObjectAnimator.ofFloat(view, "scaleX", view, "scaleY", 0.5f, 1f) playTogether(alpha, scale) duration = 300 interpolator = DecelerateInterpolator() } } } adapter.itemAnimation = ComplexAnimation() ``` ``` -------------------------------- ### Handling Not-Full-Page Scenario Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/TrailingLoadStateAdapter.md Provides an example of how to check and disable the load more functionality if the content does not fill the entire screen, preventing premature loading. ```kotlin // Handle not-full-page scenario fun submitPartialPage(items: List) { contentAdapter.submitList(currentList + items) // Disable load more if content not full screen trailingAdapter.checkDisableLoadMoreIfNotFullPage() } ``` -------------------------------- ### Implement Drag and Drop Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/INDEX.md This section guides on implementing drag and drop functionality for list items. It references `QuickDragAndSwipe` and `OnItemDragListener`. ```APIDOC ## Drag and Drop Implementation ### Description Implement drag and drop functionality for list items. This involves using the `QuickDragAndSwipe` class and implementing the `OnItemDragListener` interface. ### Method Refer to `QuickDragAndSwipe` and `OnItemDragListener` documentation. ### Endpoint `api-reference/QuickDragAndSwipe.md` ### Parameters Refer to the respective API documentation for `QuickDragAndSwipe` and `OnItemDragListener`. ### Request Example Refer to the respective API documentation for `QuickDragAndSwipe` and `OnItemDragListener`. ### Response Refer to the respective API documentation for `QuickDragAndSwipe` and `OnItemDragListener`. ``` -------------------------------- ### Use Data Binding Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/INDEX.md This section guides on integrating data binding with the adapter. It points to the `DataBindingHolder` class. ```APIDOC ## Data Binding Integration ### Description Integrate data binding for more efficient and declarative UI updates within the adapter. Use the `DataBindingHolder` class for this purpose. ### Method `DataBindingHolder` ### Endpoint `api-reference/DataBindingHolder.md` ### Parameters Refer to the `DataBindingHolder.md` documentation for implementation details. ### Request Example Refer to the `DataBindingHolder.md` documentation for usage examples. ### Response Refer to the `DataBindingHolder.md` documentation for details on data binding usage. ``` -------------------------------- ### QuickDragAndSwipe Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickDragAndSwipe.md Demonstrates how to initialize and configure QuickDragAndSwipe for a RecyclerView. Includes setting drag and swipe move flags, enabling/disabling gestures, attaching to the RecyclerView, and setting up item drag and swipe listeners. Also shows programmatic initiation of drag and swipe actions. ```kotlin class ItemAdapter : BaseQuickAdapter(...) { // ... adapter implementation } val adapter = ItemAdapter() val dragSwipe = QuickDragAndSwipe() // Configure drag and swipe dragSwipe .setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN) .setSwipeMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) .setLongPressDragEnabled(true) .setItemViewSwipeEnabled(true) .attachToRecyclerView(recyclerView) // Set drag listener dragSwipe.setOnItemDragListener(object : OnItemDragListener { override fun onItemDragStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) { Log.d("Drag", "Drag started at $pos") } override fun onItemDragMoving( source: RecyclerView.ViewHolder, from: Int, target: RecyclerView.ViewHolder, to: Int ) { Log.d("Drag", "Moving from $from to $to") } override fun onItemDragEnd(viewHolder: RecyclerView.ViewHolder, pos: Int) { Log.d("Drag", "Drag ended at $pos") } }) // Set swipe listener dragSwipe.setOnItemSwipeListener(object : OnItemSwipeListener { override fun onItemSwipeStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) { Log.d("Swipe", "Swipe started at $pos") } override fun onItemSwipeEnd(viewHolder: RecyclerView.ViewHolder, pos: Int) { Log.d("Swipe", "Swipe ended at $pos") } override fun onItemSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int, pos: Int) { Log.d("Swipe", "Swiped at $pos, direction: $direction") // Remove from list adapter.removeAt(pos) } override fun onItemSwipeMoving( canvas: Canvas, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, isCurrentlyActive: Boolean ) { // Custom drawing during swipe (e.g., highlight background) } }) // Programmatic drag/swipe binding.button.setOnClickListener { dragSwipe.startDrag(0) // Drag first item } binding.buttonSwipe.setOnClickListener { dragSwipe.startSwipe(1) // Swipe second item } ``` -------------------------------- ### Managing Load State in ViewModel/Activity Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LeadingLoadStateAdapter.md Control the load state of the leading adapter from your ViewModel or Activity. This example shows how to manually set the load state to Loading, NotLoading, or Error, and how to update the content adapter. ```kotlin fun loadPreviousPage() { // Show loading leadingAdapter.loadState = LoadState.Loading // Fetch data... // On success contentAdapter.submitList(newItems + currentList) leadingAdapter.loadState = LoadState.NotLoading(endOfPaginationReached = false) // On error (hidden by default, but still trackable) leadingAdapter.loadState = LoadState.Error(exception) } ``` -------------------------------- ### Typical Pagination State Transitions Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/LoadState.md Illustrates a common sequence of LoadState changes during a successful pagination process, starting from None and ending with NotLoading(true). ```text None ↓ NotLoading(endOfPaginationReached=false) ↓ Loading ↓ NotLoading(endOfPaginationReached=false) [more data available] ↓ Loading ↓ NotLoading(endOfPaginationReached=true) [all data loaded] ``` -------------------------------- ### QuickViewHolder Usage Example Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/QuickViewHolder.md Demonstrates how to use QuickViewHolder within a custom RecyclerView.ViewHolder to bind data, including setting text, colors, images, visibility, and states. ```kotlin class ItemViewHolder(itemView: View) : QuickViewHolder(itemView) { fun bind(item: Item) { // Set text setText(R.id.title, item.title) setText(R.id.description, R.string.desc_template) // Set colors setTextColor(R.id.title, Color.BLACK) setTextColorRes(R.id.subtitle, R.color.gray) // Set images setImageResource(R.id.icon, R.drawable.ic_item) // Control visibility setVisible(R.id.extra, item.hasExtra) setGone(R.id.loading, !item.isLoading) // Set state setEnabled(R.id.button, !item.isDisabled) setSelected(R.id.checkbox, item.isSelected) } } // In adapter class MyAdapter : BaseQuickAdapter() { override fun onCreateViewHolder( context: Context, parent: ViewGroup, viewType: Int ): ItemViewHolder { return ItemViewHolder(parent.getItemView(R.layout.item_layout)) } override fun onBindViewHolder(holder: ItemViewHolder, position: Int, item: Item?) { item?.let { holder.bind(it) } } } ``` -------------------------------- ### Custom Trailing Adapter Implementation Source: https://github.com/cymchad/baserecyclerviewadapterhelper/blob/master/_autodocs/api-reference/TrailingLoadStateAdapter.md Example of creating a custom TrailingLoadStateAdapter. This involves defining ViewHolder creation and binding logic based on the LoadState. ```kotlin // Create custom trailing adapter class CustomTrailingAdapter : TrailingLoadStateAdapter(isLoadEndDisplay = true) { override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadingVH { return LoadingVH(parent.getItemView(R.layout.item_loading)) } override fun onBindViewHolder(holder: LoadingVH, loadState: LoadState) { when (loadState) { is LoadState.Loading -> holder.showLoading() is LoadState.Error -> holder.showError(loadState.error.message) is LoadState.NotLoading -> { if (loadState.endOfPaginationReached) { holder.showEnd() } else { holder.hide() } } else -> holder.hide() } } inner class LoadingVH(itemView: View) : RecyclerView.ViewHolder(itemView) { fun showLoading() { itemView.findViewById(R.id.progress).visibility = View.VISIBLE } fun showError(message: String?) { itemView.findViewById(R.id.error_text).text = message itemView.findViewById