How to write less Boilerplate to make an adapter for simple recycler view

abdomi7

AbdelHalim Mahmoud

Posted on May 24, 2023

How to write less Boilerplate to make an adapter for simple recycler view

As developers, we know that writing code can be both rewarding and challenging. While creating new features and functionality is exciting, dealing with repetitive and time-consuming tasks can quickly become frustrating. One area where this is particularly true is in writing adapters for simple Recycler Views. But what if there was a way to eliminate this boilerplate code and streamline the process? That's where my new library comes in.

The library is designed to help developers save time and increase productivity by eliminating the need to write boilerplate adapters for simple Recycler Views. With this solution, you can focus on building out more meaningful and impactful features for your application without getting bogged down in repetitive tasks.

But what are some of the specific benefits of using the library? Let's take a look:

  1. Increased Productivity
    By removing the need to write boilerplate adapter code, the library helps developers save time and work more efficiently. Rather than spending hours writing and debugging the same code over and over again, you can use the library to generate clean, concise adapters in a fraction of the time.

  2. Reduced Errors
    When dealing with repetitive tasks, it's easy to make mistakes or forget important details. This can lead to bugs and issues that are difficult to track down. By using this library, you can minimize the risk of errors and improve the overall quality of your code. The adapters generated by the library have been thoroughly tested and are designed to work seamlessly with Recycler Views, so you can focus on building out your application without worrying about technical details.

  3. Improved Code Readability
    Writing clean, readable code is important not just for you, but for other developers who may need to work on your code in the future. By using this library, you can create adapters that are easy to read and understand, even for developers who are new to your codebase. This can be particularly beneficial when working on a team, where clear communication and readability are key.

Getting Started
So how do you get started with this library? It's easy!

Step 1. Add the JitPack repository to your build file

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the library dependency

Add the following dependency to your app's build.gradle file:

dependencies {
            implementation 'com.github.abdomi7:BoilrBite:1.0.7'
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Start using BoilrBite

Create an instance of BoilrBite adapter and set it to your RecyclerView:

    val adapter = BoilrBite.createBoilrBiteAdapter(
            items = mutableListOf<DummyModel>(), // List of items to be displayed
            layoutResId = R.layout.item, // Layout resource id
            clickableViewIds = setOf(R.id.btn_dummy, R.id.tv_name, R.id.tv_university), // Set of clickable view ids
            compareItems = { old, new -> old == new }, // Compare items to check for changes
            compareContents = { old, new -> old.id == new.id }, // Compare contents to check for changes
            bind = { view, item -> // Bind the item to the view
                val binding = DataBindingUtil.bind<ItemBinding>(view)
                binding?.tvName?.text = item.name
                binding?.tvUniversity?.text = item.university
            },
            onViewRecycled = { view, item ->
                val binding = DataBindingUtil.getBinding<ItemBinding>(view)
                binding?.unbind()
            })

    binding.rvMain.adapter = adapter // Set the adapter to the RecyclerView

Enter fullscreen mode Exit fullscreen mode

That is it!

Common functions for data manipulation

BoilrBite provides many useful functions for data manipulation:


    adapter.setItems(listOf(DummyModel())) // Set the list of items

    adapter.addOrUpdateItems(listOf(DummyModel())) // Add or update items in the list

    adapter.addOrUpdateItem(DummyModel()) // Add or update a single item in the list

    adapter.clearItems() // Clear all items in the list

    adapter.setSelectedItem(0) // Set the selected item

    adapter.getSelected() // Get the selected item position

    adapter.getSelectedItem() // Get the selected item

    adapter.removeItem(0) // Remove item at index 0

    adapter.removeItems() // Remove items from the list

    adapter.clear() // Clear the list

    adapter.indexOf(DummyModel()) // Get the index of an item


Enter fullscreen mode Exit fullscreen mode

Event Handling

BoilrBite makes event handling easy with the following features:

        adapter.onItemClickListener = object : OnItemClickListener<DummyModel, View?>() {
            override fun onItemClicked(view: View?, item: DummyModel, position: Int) {
                Toast.makeText(this@MainActivity, "Item clicked: $item", Toast.LENGTH_SHORT).show()
            }
        } // Set the item click listener


        adapter.onSelectedItemChange =
            BoilrBite.OnSelectedItemChange<DummyModel> { oldPosition, oldItem, newPosition, newItem ->
                Toast.makeText(
                    this@MainActivity,
                    "new: " + newItem?.id.toString() + "  old: " + oldItem?.id.toString(),
                    Toast.LENGTH_SHORT
                ).show()
            } // Set the selected item change listener

Enter fullscreen mode Exit fullscreen mode

Repo Link: BoilrBite

Conclusion

Overall, using this library to eliminate boilerplate adapter code for simple Recycler Views can help you save time, reduce errors, and improve code readability. If you're tired of writing the same code over and over again and want to focus on more meaningful aspects of your application, consider giving this library a try. I think you'll be pleasantly surprised by how much time and effort it can save you in the long run

💖 💪 🙅 🚩
abdomi7
AbdelHalim Mahmoud

Posted on May 24, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related