title: PagedListAdapter Not Using DiffUtil On invalidate Data
body: <p>Every time I call invalidate data my DIFF_UTIL is not used. The logs are not shown and the entire list is updated with new data causing the screen to move positions etc. Not sure what the issue is here.</p>
  
  <p>I have <code>PagedListAdapter</code> with a <code>LiveData&lt;PagedList&lt;Post&gt;&gt; postList</code> and I call <code>postList.getValue().getDataSource().invalidate();</code> to refresh my data.</p>
  
  <p>I have <code>DIFF_UTIL</code></p>
  
  <pre><code> public static DiffUtil.ItemCallback&lt;Post&gt; DIFF_CALLBACK =
              new DiffUtil.ItemCallback&lt;Post&gt;() {
  
                  @Override
                  public boolean areItemsTheSame(Post oldItem, Post newItem) {
                      Log.d(TAG, "areItemsTheSame with result: "
                              + (oldItem.getId() == newItem.getId()));
                      Log.d(TAG, "areItemsTheSame Old Item is: " + oldItem.toString());
                      Log.d(TAG, "areItemsTheSame New Item is: " + newItem.toString());
                      return oldItem.getId() == newItem.getId();
                  }
  
                  @Override
                  public boolean areContentsTheSame(Post oldItem, Post newItem) {
                      Log.d(TAG, "areContentsTheSame with result: " + oldItem.equals(newItem));
                      Log.d(TAG, "areContentsTheSame Old Item is: " + oldItem.toString());
                      Log.d(TAG, "areContentsTheSame New Item is: " + newItem.toString());
                      return oldItem.equals(newItem);
                  }
              };
  </code></pre>
  
  <p>I have my Adapter:</p>
  
  <pre><code>import...
  
  public class PostAdapter extends PagedListAdapter&lt;Post, PostAdapter.PostViewHolder&gt; {
      interface...
  
      protected PostAdapter() {
          super(DIFF_CALLBACK);
      }
  
      @NonNull
      @Override
      public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater
                  .from(parent.getContext()).inflate(R.layout.feed_card_view, parent, false);
          return new PostViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
          Post post = getItem(position);
          Log.d(TAG, "onBindViewHolder with position: " + position);
          if (post != null) {
              setUpPost(holder, post);
              Log.d(TAG, "onBindViewHolder with post: " + post.toString());
          } else {
              holder.clear();
              Log.d(TAG, "onBindViewHolder post is null");
          }
      }
  
      private void setUpPost(@NonNull PostViewHolder holder, @NonNull Post post) {
         ...do some things to set up post
      }
  
      class PostViewHolder extends RecyclerView.ViewHolder {
         Buttons etc..
  
          public PostViewHolder(View itemView) {
              super(itemView);
              button b = findviewbyid...
          }
  
          void clear() {
              button.invalidate()..
          }
      }
  }
  
  </code></pre>
  
  <p>My Fragment</p>
  
  <pre><code> @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View v = inflater.inflate(R.layout.fragment_feed, container, false);
          feedViewModel = ViewModelProviders.of(getActivity()).get(FeedViewModel.class);
  
          RecyclerView recyclerView = v.findViewById(R.id.post_list);
          PostAdapter adapter = new PostAdapter(this);
  
          feedViewModel.postList.observe(this, pagedList -&gt; {
              try {
                  //refresh current list
                  adapter.submitList(pagedList);
              } catch (Exception e) {}
          });
  
  
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
  
          return v;
      }
  </code></pre>
  
  <p>My ViewModel</p>
  
  <pre><code>public class FeedViewModel extends AndroidViewModel {
      private LiveData&lt;PagedList&lt;Post&gt;&gt; postList;
      private FeedRepository feedRepository;
  
      public FeedViewModel(final Application application) {
          super(application);
          feedRepository = new FeedRepository();
          postList = feedRepository.getPosts();
      }
  
      public void refreshPosts() {
          feedRepository.refreshPosts();
      }
  
      ... 
  
      public LiveData&lt;PagedList&lt;Post&gt;&gt; getPostList() {
          return postList;
      }
  }
  </code></pre>
  
  <p>The Repository</p>
  
  <pre><code>public class FeedRepository {
      private LiveData&lt;PagedList&lt;Post&gt;&gt; postList;
  
      private static final String TAG = FeedRepository.class.getName();
      private static final int NUMBER_OF_POSTS_TO_GET = 2;
      private static final boolean ENABLE_POST_PAGE_HOLDER = true;
      private static final int NUMBER_OF_POSTS_TO_GET_IN_ADVANCE = 50;
      private static final int NUMBER_OF_POSTS_ON_FIRST_LOAD = 20;
  
      public FeedRepository() {
          //Prefs on PagedList
          PagedList.Config myPagingConfig = new PagedList.Config.Builder()
                  .setPageSize(NUMBER_OF_POSTS_TO_GET)
                  .setEnablePlaceholders(ENABLE_POST_PAGE_HOLDER)
                  .setPrefetchDistance(NUMBER_OF_POSTS_TO_GET_IN_ADVANCE)
                  .setInitialLoadSizeHint(NUMBER_OF_POSTS_ON_FIRST_LOAD)
                  .build();
  
          PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory();
  
          postList = new LivePagedListBuilder(
                  postDataSourceFactory, myPagingConfig).build();
      }
  
      public LiveData&lt;PagedList&lt;Post&gt;&gt; getPosts() {
          return postList;
      }
  
      public void refreshPosts() {
          Log.d(TAG, "refreshPosts, invalidateDataSources()");
          postList.getValue().getDataSource().invalidate();
      }
  }
  
  </code></pre>
  
  <p>DataSource.Factory</p>
  
  <pre><code>public class PostDataSourceFactory extends DataSource.Factory&lt;String, Post&gt; {
     public PostDataSourceFactory() {
      }
  
      @NonNull
      @Override
      public DataSource&lt;String, Post&gt; create() {
          PostDataSource postDataSource = new PostDataSource();
          return postDataSource;
      }
  }
  </code></pre>
  
  <p>I following the google example here: <a href="https://developer.android.com/reference/android/arch/paging/PagedListAdapter.html" rel="noreferrer">https://developer.android.com/reference/android/arch/paging/PagedListAdapter.html</a></p>