title: getItemCount() return only the original Content of RecyclerView
body: <p>Please don't mark this as duplicate. I have search &amp; didn't got my answer. Please help me. </p>
  
  <p>My Question is I have used a RecyclerView where I am adding the contacts details and displaying. I passed the actual data as ArrayList to MyAdapter class. there I have created another ArrayList and copy all the items of Original List into that List and performing all the necessary operations like adding removing and moving etc. This also works fine for me. But when I search something that is not available in the list I want to display some text message like "No search record found". I have done that thing in my onCreateViewHolder() method. <strong>If there is no elements found then also getItemCount() returning the original content. How could I achieve this. Please help me.</strong></p>
  
  <p>This is where I am passing the actual data to my adapter class</p>
  
  <pre><code>adapter = new MyAdapter(MainActivity.this, modelData);
  mRecyclerView.setAdapter(adapter);
  mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
  </code></pre>
  
  <p>Then using onQueryTextChange() I am searching the elements in RecyclerView.</p>
  
  <pre><code>@Override
  public boolean onQueryTextChange(String newText) {
      List&lt;MyModel&gt; filteredModelList = filter(modelData, newText);
      if(filteredModelList.size()&gt;0) {
          adapter.animateTo(filteredModelList);
          mRecyclerView.scrollToPosition(0);
          return true;
      }else{
          Toast.makeText(this,"EmptyList",Toast.LENGTH_SHORT).show();
          return true;
      }
  }
  
  private List&lt;MyModel&gt; filter(List&lt;MyModel&gt; models, String query) {
      query = query.toLowerCase();
  
      final List&lt;MyModel&gt; filteredModelList = new ArrayList&lt;&gt;();
      for (MyModel model : models) {
          final String text = model.getName().toLowerCase();
       if (text.contains(query) || phone.contains(query)) {
              filteredModelList.add(model);
  
          } else {
  
          }
      }
      return filteredModelList;
  }
  </code></pre>
  
  <p>Then in my adapter constructor in am creating another arraylist and copying the actual data into this arraylist.</p>
  
  <pre><code>public MyAdapter(Context context, List&lt;MyModel&gt; mModels) {
  
      myModels = new ArrayList&lt;&gt;(mModels);
      this.context = context;
    }
  </code></pre>
  
  <p>I have used getCount() like this.</p>
  
  <pre><code>@Override
  public int getItemCount() {
      Log.e("Size", "" + myModels.size());
      if (myModels.size() &gt; 0) {
          Log.e("Size", "" + myModels.size());
          return myModels.size();
      }
  
  
      return 1;
  }
  
  
  @Override
  public int getItemViewType(int position) {
      if (myModels.size() == 0) {
          return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
      } else {
          return VIEW_TYPE_OBJECT_VIEW;
      }
  }
  </code></pre>
  
  <p>I am doing search operation on duplicate copy of arraylist. The searching is fine. but when the search doesn't match it should display some seperate layout saying "No result found" (or some error).  I have done that like this.</p>
  
  <pre><code>@Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v;
      switch (viewType) {
          case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
              // return view holder for your placeholder
              v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
              EmptyViewHolder evh = new EmptyViewHolder(v);
              return evh;
  
          case VIEW_TYPE_OBJECT_VIEW:
              // return view holder for your normal list item
              v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
              ViewHolder vh = new ViewHolder(v);
              return vh;
  
      }
      return null;
  }
  </code></pre>
  
  <p>Here is MyAdapter Class</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  
  
  private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
  private static final int VIEW_TYPE_OBJECT_VIEW = 1;
  List&lt;MyModel&gt; myModels;
  Context context;
  
  public MyAdapter(Context context, List&lt;MyModel&gt; mModels) {
  
      myModels = new ArrayList&lt;&gt;(mModels);
      this.context = context;
  }
  
  
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v;
      switch (viewType) {
          case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
              // return view holder for your placeholder
              v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
              EmptyViewHolder evh = new EmptyViewHolder(v);
              return evh;
  
          case VIEW_TYPE_OBJECT_VIEW:
              // return view holder for your normal list item
              v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
              ViewHolder vh = new ViewHolder(v);
              return vh;
  
      }
      return null;
  }
  
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  
      if (holder instanceof ViewHolder) {
          ViewHolder vh = (ViewHolder) holder;
          MyModel model = myModels.get(position);
          vh.bind(model);
      }
  
  
  }
  
  @Override
  public int getItemCount() {
      Log.e("Size", "" + myModels.size());
      if (myModels.size() &gt; 0) {
          Log.e("Size", "" + myModels.size());
          return myModels.size();
      }
  
      else if(removeModel.size()==0){
          Log.e("Size remove", "" + removeModel.size());
          Log.e("","");
          return 1;
      }
      return 0;
  }
  
  
  @Override
  public int getItemViewType(int position) {
      if (myModels.size() == 0) {
          return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
      } else {
          return VIEW_TYPE_OBJECT_VIEW;
      }
  }
  
  public void animateTo(List&lt;MyModel&gt; models) {
  
      applyAndAnimateRemovals(models);
      applyAndAnimateAdditions(models);
      applyAndAnimateMovedItems(models);
  
  }
  
  private void applyAndAnimateRemovals(List&lt;MyModel&gt; newModels) {
      if (newModels.size() &gt; 0) {
          for (int i = myModels.size() - 1; i &gt;= 0; i--) {
              final MyModel model = myModels.get(i);
              if (!newModels.contains(model))
                  removeItem(i);
          }
      } else {
          removeModel = new ArrayList&lt;&gt;(myModels);
          Log.e("removeModel 1", "" + removeModel);
          for (int i = removeModel.size() - 1; i &gt;= 0; i--) {
              removeAllItem(i);
          }
          Log.e("removeModel 2    ", "" + removeModel);
  
          Log.e("myModels", "" + myModels);
      }
  }
  
  private void applyAndAnimateAdditions(List&lt;MyModel&gt; newModels) {
      for (int i = 0, count = newModels.size(); i &lt; count; i++) {
          final MyModel model = newModels.get(i);
          if (!myModels.contains(model)) {
              addItem(i, model);
          }
      }
  }
  
  private void applyAndAnimateMovedItems(List&lt;MyModel&gt; newModels) {
      for (int toPosition = newModels.size() - 1; toPosition &gt;= 0; toPosition--) {
          final MyModel model = newModels.get(toPosition);
          final int fromPosition = myModels.indexOf(model);
          if (fromPosition &gt;= 0 &amp;&amp; fromPosition != toPosition) {
              moveItem(fromPosition, toPosition);
          }
      }
  }
  
  public MyModel removeAllItem(int position) {
      MyModel model = removeModel.remove(position);
      notifyItemRemoved(position);
      return model;
  }
  
  public MyModel removeItem(int position) {
      final MyModel model = myModels.remove(position);
      notifyItemRemoved(position);
      return model;
  }
  
  public void addItem(int position, MyModel model) {
      myModels.add(position, model);
      notifyItemInserted(position);
  }
  
  public void moveItem(int fromPosition, int toPosition) {
      final MyModel model = myModels.remove(fromPosition);
      myModels.add(toPosition, model);
      notifyItemMoved(fromPosition, toPosition);
  }
  
  public class EmptyViewHolder extends RecyclerView.ViewHolder {
      TextView textView;
  
      public EmptyViewHolder(View itemView) {
          super(itemView);
          textView = (TextView) itemView.findViewById(R.id.no_contact);
          textView.setText("No Contacts to Display");
      }
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
      TextView tvName;
      TextView tvPhoneNumber;
      ImageView ivImage;
  
      public ViewHolder(View itemView) {
          super(itemView);
          tvName = (TextView) itemView.findViewById(R.id.tv_name);
          tvPhoneNumber = (TextView) itemView.findViewById(R.id.phone_number);
          ivImage = (ImageView) itemView.findViewById(R.id.ivPic);
      }
  
      public void bind(MyModel model) {
  
          Log.e("Name ", model.getName());
          tvName.setText(model.getName());
          tvPhoneNumber.setText(model.getPhoneNumber());
          Log.e("Image getImagePath()", model.getImagePath());
          Log.e("Image URI()", "" + Uri.parse(model.getImagePath()));
          if (model.getImagePath().startsWith("/")) {
              ivImage.setImageURI(Uri.parse(model.getImagePath()));
          } else {
              ivImage.setImageResource(R.drawable.default_contact_pic);
          }
      }
  }
  </code></pre>