title: Android Architecture Components: using ViewModel for RecyclerView items
body: <p>I'm experimenting with the Architecture Components, and I want to build a ViewModel for each item of a RecyclerView. I'm not sure if that is formally correct or I should stick with the "old way".</p>
  
  <p>I have this adapter: </p>
  
  <pre class="lang-java prettyprint-override"><code>public class PostAdapter extends RecyclerView.Adapter&lt;PostAdapter.PostViewHolder&gt; {
  
      private List&lt;Post&gt; list;
      public static class PostViewHolder extends RecyclerView.ViewHolder{
          final ItemPostBinding binding;
  
          public PostViewHolder(ItemPostBinding binding){
              super(binding.getRoot());
              this.binding = binding;
          }
      }
  
      @Override
      public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          ItemPostBinding binding = DataBindingUtil
                  .inflate(LayoutInflater.from(parent.getContext()), R.layout.item_post,
                          parent, false);
  
  
          return new PostViewHolder(binding, parent.getContext());
      }
  
      @Override
      public void onBindViewHolder(PostViewHolder holder, int position) {
          holder.binding.setPost(list.get(position));
          holder.binding.executePendingBindings();
      }
  
      @Override
      public int getItemCount() {
          return list == null ? 0 : list.size();
      }
  
      public void setList(List&lt;Post&gt; list){
          this.list = list;
          notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>which works fine but it's very basic. how do I update it so each item has it's own ViewModel associated? is that even possible?</p>
  
  <p>EDIT: playing with it, I've tried to put in ViewModels the following way:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class PostAdapter extends RecyclerView.Adapter&lt;PostAdapter.PostViewHolder&gt; {
  
      private List&lt;Post&gt; list;
      public static class PostViewHolder extends RecyclerView.ViewHolder{
          final ItemPostBinding binding;
          private final Context context;
          private GalleryItemViewModel viewModel;
  
          public PostViewHolder(ItemPostBinding binding, Context context){
              super(binding.getRoot());
              this.binding = binding;
              this.context = context;
          }
  
          public Context getContext(){
              return context;
          }
  
          public void setViewModel(GalleryItemViewModel viewModel){
              this.viewModel = viewModel;
              binding.setViewModel(viewModel);
          }
      }
  
      @Override
      public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          ItemPostBinding binding = DataBindingUtil
                  .inflate(LayoutInflater.from(parent.getContext()), R.layout.item_post,
                          parent, false);
  
  
          return new PostViewHolder(binding, parent.getContext());
      }
  
      @Override
      public void onBindViewHolder(PostViewHolder holder, int position) {
          GalleryItemViewModel vm = ViewModelProviders.of((FragmentActivity) holder.getContext()).get(GalleryItemViewModel.class);
          vm.setPost(list.get(position));
          holder.setViewModel(vm);
      }
  
      @Override
      public int getItemCount() {
          return list == null ? 0 : list.size();
      }
  
      public void setList(List&lt;Post&gt; list){
          this.list = list;
          notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>it works but is that the correct way to do it?</p>