BaseRecyclerViewAdapterHelper (BRVAH)

repository·master·Indexed 12 days ago

https://github.com/cymchad/baserecyclerviewadapterhelper

A flexible Android library designed to simplify RecyclerView adapter implementation. BRVAH provides support for multi-type layouts, pagination (up/down loading), and seamless integration with ConcatAdapter. Version 4.4.1 introduces a modular design, the QuickAdapterHelper utility for managing adapter composition, and a LoadState system to track data fetching operations.

Tokens
1.7K
Snippets
3
Records
9
Agent score
97%

What's inside BRVAH

  1. Overview of BaseRecyclerViewAdapterHelper (BRVAH)

    master

    BRVAH is a powerful and flexible RecyclerView Adapter helper for Android. The v4 release introduces several improvements:

    • ConcatAdapter Compatibility: Perfect compatibility with Android's ConcatAdapter.
    • Modular Design: Functionality has been split into modules, making the BaseAdapter cleaner and more concise.
    • Flexible Multi-type Layouts: Enhanced support for handling multiple item types.
    • Improved Loading: Significantly strengthened support for upward and downward loading (pagination/infinite scroll).

    Developers can choose to use the v4 version or continue using the 2.x or 3.x branches.

  2. Configure Proguard rules for BRVAH

    master
    The library includes its own proguard-rules.pro which is automatically imported during the build process. In most cases, you do not need to manually add any rules to your project's Proguard configuration. If you need to inspect the rules, they are located in the library's source.
  3. Install BaseRecyclerViewAdapterHelper4

    master

    To use the latest version (v4.x.x) of BRVAH, add the following dependency to your build.gradle file. The v4 version is available on Maven Central, so no additional repository configurations are required.

    implementation("io.github.cymchad:BaseRecyclerViewAdapterHelper4:4.4.1")
  4. Manage adapter loading states with LoadState

    master

    The LoadState sealed class represents the current status of data loading operations within the adapter. It is used to track whether the adapter is currently fetching data, has finished loading, or has encountered an error. Every state includes an endOfPaginationReached boolean property, which indicates if the data source has no more items to load.

    Available states:

    • None: No status currently exists (e.g., during initialization or a refresh).
    • NotLoading: Loading is not in progress and no error has occurred. Use the companion constants NotLoading.Complete (when pagination is finished) or NotLoading.Incomplete (when more data is available).
    • Loading: A load operation is currently in progress.
    • Error: A load operation failed. This state contains the error property (a Throwable) describing the failure.
  5. Use QuickAdapterHelper to manage ConcatAdapter

    master

    QuickAdapterHelper is a utility class used to compose multiple adapters into a single ConcatAdapter. It is specifically designed to manage a main contentAdapter along with optional leadingLoadStateAdapter (for loading more at the top) and trailingLoadStateAdapter (for loading more at the bottom), as well as any additional adapters you want to insert before or after the content.

    The resulting adapter structure is:

    1. leadingLoadStateAdapter (optional) OR BeforeAdapters
    2. contentAdapter (the main data)
    3. trailingLoadStateAdapter (optional) OR AfterAdapters
    val helper = QuickAdapterHelper.Builder(myContentAdapter)
        .setTrailingLoadStateAdapter { /* handle load more */ }
        .build()
    
    recyclerView.adapter = helper.adapter
  6. Configure QuickAdapterHelper using Builder

    master

    To create a QuickAdapterHelper, use the QuickAdapterHelper.Builder initialized with your main contentAdapter.

    Key configuration methods:

    • setLeadingLoadStateAdapter(loadStateAdapter: LeadingLoadStateAdapter<*>?): Set a custom adapter for leading load states.
    • setLeadingLoadStateAdapter(loadListener: LeadingLoadStateAdapter.OnLeadingListener?): Set a listener for leading load events (uses a default adapter).
    • setTrailPreloadSize(size: Int): Sets how many items from the tail should trigger a preload.
    • isTrailAutoLoadMore(enable: Boolean): Enables or disables automatic loading more at the tail.
    • setTrailingLoadStateAdapter(loadStateAdapter: TrailingLoadStateAdapter<*>?): Set a custom adapter for trailing load states.
    • setTrailingLoadStateAdapter(loadMoreListener: TrailingLoadStateAdapter.OnTrailingListener?): Set a listener for trailing load events (uses a default adapter).
    • setConfig(config: ConcatAdapter.Config): Set the configuration for the underlying ConcatAdapter.
    • attachTo(recyclerView: RecyclerView): A convenience method that builds the helper and immediately assigns the resulting adapter to the provided RecyclerView.
    QuickAdapterHelper.Builder(contentAdapter)
        .setTrailPreloadSize(5)
        .isTrailAutoLoadMore(true)
        .setTrailingLoadStateAdapter { /* load more logic */ }
        .attachTo(recyclerView)
  7. Manage Loading States in QuickAdapterHelper

    master

    You can manually control the loading states for the head (leading) and tail (trailing) of your list through the QuickAdapterHelper instance. This updates the state within the respective LoadStateAdapter.

    • leadingLoadState: Gets or sets the LoadState for the head. If no leading adapter is present, it returns LoadState.NotLoading(endOfPaginationReached = false).
    • trailingLoadState: Gets or sets the LoadState for the tail. If no trailing adapter is present, it returns LoadState.NotLoading(endOfPaginationReached = false).
  8. Add and remove adapters relative to contentAdapter

    master

    You can dynamically manage adapters positioned around your main contentAdapter using the following methods:

    • addBeforeAdapter(adapter: BaseQuickAdapter<*, *>): Adds an adapter at the very beginning of the sequence.
    • addBeforeAdapter(index: Int, adapter: BaseQuickAdapter<*, *>): Adds an adapter at a specific index relative to the contentAdapter.
    • addAfterAdapter(adapter: BaseQuickAdapter<*, *>): Adds an adapter immediately after the contentAdapter.
    • addAfterAdapter(index: Int, adapter: BaseQuickAdapter<*, *>): Adds an adapter at a specific index relative to the contentAdapter.
    • removeAdapter(adapter: BaseQuickAdapter<*, *>): Removes a specific adapter from the helper (cannot remove the contentAdapter).
    • clearBeforeAdapters(): Removes all adapters added via addBeforeAdapter.
    • clearAfterAdapters(): Removes all adapters added via addAfterAdapter.

    Accessing lists:

    • beforeAdapterList: Returns a read-only list of adapters positioned before the content.
    • afterAdapterList: Returns a read-only list of adapters positioned after the content.
  9. Use LoadState subclasses and constants

    master

    When handling adapter states, you can use the following LoadState types:

    TypeDescription
    LoadState.NoneInitial or refreshing state.
    LoadState.LoadingCurrently fetching data.
    LoadState.NotLoading.CompleteFinished loading and reached the end of the list.
    LoadState.NotLoading.IncompleteFinished loading but more data is available.
    LoadState.ErrorAn error occurred during loading. Access the error property to inspect the Throwable.