RecyclerViewUtils Library Released

Recently, I became tired of writing the same old tedious code for every single RecyclerView and Adpater class I used, that all did the same thing, so I extrapolated all of it into a personal library.

The RecyclerViewUtils library helps make everyone’s life a little easier with a CoreViewHolder and CoreAdapter class, described below.

CoreViewHolder

At the heart of these classes is CoreViewHolder which is a RecyclerView.ViewHolder class used to display an object of a specific type. It has one abstract method for binding an object of that type.

Here is a sample of a CoreviewHolder for Account objects:

   public class AccountViewHolder extends CoreViewHolder<Account> {
      private TextView tvName;
      private TextView tvBalance;
    
      public AccountViewHolder(View view) {
         super(view);
    
         this.tvName = (TextView) view.findViewById(R.id.account_name);
         this.tvBalance = (TextView) view.findViewById(R.id.account_balance);
      }
    
      @Override
         protected void bindItem(Account item) {
         this.tvName.setText(item.getName());
         this.tvBalance.setText(String.valueOf(item.getBalance()));
      }
   }

CoreRecyclerViewAdapter

Using the CoreViewHolder class from above, the CoreRecyclerViewAdapter is an abstract base class for using an adapter of objects with a given type. By using a generic type, we were able to override many boilerplate methods such as:

  • add
  • remove
  • swapItems
  • onBindViewHolder

Thanks to this handy utils class, it cuts down on a ton of boilerplate code inside your adapter, and make it very simple:

   public class AccountAdapter extends CoreRecyclerViewAdapter<Account, AccountAdapter.AccountViewHolder>{
      public AccountAdapter(Context context, List<Account> accounts) {
         super(context, accounts);
      }
    
      @Override
      public AccountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         return new AccountViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_account, parent, false));
      }
    
      public class AccountViewHolder extends CoreViewHolder<Account> {
         ...
      }
   }

Adam McNeilly

Adam McNeilly
Software Engineer and Android development enthusiast.

Breaking the Buzzwords Barrier Part 3: ViewModel

Explains what a ViewModel means in both MVVM and Android contexts. Continue reading