title: OnBindViewholder Not Returning Exact Position After Scroll
body: <p>I am trying to replicate YouTube's Homepage view with a horizontal scroll at the top for the trending topics and then a set of card views to show the videos with a footer view to show home, subscriptions and the lot. </p>
  
  <p>I am using a <code>Recyclerview</code> with a Header which again has a <code>RecyclerView</code> where for the top scrolling of the trending topics and a fixed <code>LinearLayout</code> at the bottom for the footer. (Not fixed at the bottom but added at the last position for now).</p>
  
  <p><strong>Issue &amp; Initial Findings</strong></p>
  
  <p>I want to have a custom header view at the 1st position (Array Index - 0) and another custom footer at the 7th position (Array Index - 6). My issue is that when I initially load the app, I can see that <code>onBindViewHolder</code> is calling the positions from 0 to 6 and when I scroll down all the way, all the views are showing fine but when I move up, I can see that there are 3 empty positions added in the positions of 1,2 and 3 and it's showing a plain white screen but I can actually see those 1,2 and 3rd items (along with the remaining as well) below the plain white space which means that a new 1,2 and 3rd position with plain white space popped up after the scroll. It's completely weird and I am not doing any actions in any of the <code>RecyclerView</code> items as well and making no changes dynamically.</p>
  
  <p>I would much appreciate anyone pointing out any dumb mistakes that I might be doing. </p>
  
  <p><strong>Main screen with fully loaded views when I first open the app</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/l8eRL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l8eRL.jpg" alt="Main Screen With Fully Loaded Views"></a></p>
  
  <p><strong>Main screen with empty views at positions 1,2 and 3 after scrolling</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/3jyju.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3jyju.jpg" alt="Main Screen With Empty Views"></a></p>
  
  <p>I have a fragment where I set the <code>RecyclerView</code> and that specific code is as follows (I am not sure if one would need this to find the issue but just in case)</p>
  
  <pre><code>View rootView = inflater.inflate(R.layout.youtube_primary_screen, container, false);
  recyclerView = rootView.findViewById(R.id.video_listview);
  mainScrollAdapter = new MainScrollAdapter(context,repeaterClasses,top_scroll_item_array);
  recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
  recyclerView.setItemAnimator(new DefaultItemAnimator());
  recyclerView.setAdapter(mainScrollAdapter);
  mainScrollAdapter.notifyDataSetChanged();
  return rootView;
  </code></pre>
  
  <p>Here is my Main Adapter class for the main <code>RecyclerView</code> which has the header &amp; footer</p>
  
  <pre><code>@NonNull
      @Override
      public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
          View v;
          this.VIEW_TYPE = viewType;
  
          switch (viewType)
          {
              case VIEW_TYPES.Header:
                  Log.e("Inside","Header");
                  v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view,parent,false);
                  break;
  
              case VIEW_TYPES.Footer:
                  Log.e("Inside","Footer");
                  v = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_view,parent,false);
                  break;
  
              default:
                  Log.e("Inside","Others");
                  v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_video_item,parent,false);
                  break;
  
          }
  
          BaseViewHolder myViewHolder = new BaseViewHolder(context,v,mYoutubeVideos,top_scroll_items);
          return myViewHolder;
      }
  
  
  
      @Override
      public void onBindViewHolder(@NonNull BaseViewHolder holder, final int position) {
          holder.onBind(position);
          Log.e("OnBindVIewHolder",""+holder.getAdapterPosition());
  
      }
  
      @Override
      public int getItemViewType(int position) {
          Log.e("getItemViewType",""+position);
          if(position==0)
              return VIEW_TYPES.Header;
          if(position==7)
              return VIEW_TYPES.Footer;
          else
              return position;
      }
  
      @Override
      public int getItemCount() {
         return 7;
      }
  
  
      private class VIEW_TYPES {
  
          public static final int Header = 0;
          public static final int Footer = 6;
      }
  </code></pre>
  
  <p>My <code>BaseViewHolder</code> that extends the <code>RecyclerView.ViewHolder</code> class whose <code>onBind</code> method is as follows</p>
  
  <pre><code>public void onBind(final int position) {
          switch(position) {
  
              case VIEW_TYPES.Header:
                  top_scroll_recycler_view.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                  top_scroll_recycler_view.setItemAnimator(new DefaultItemAnimator());
                  top_scroll_recycler_view.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL) {
                      @Override
                      public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                          // Do not draw the divider
                      }
                  });
                  top_scroll_recycler_view.setAdapter(new TopScrollAdap(context,top_scroll_items));
                  break;
  
              case VIEW_TYPES.Footer:
                  break;
  
              default:
                  Log.e("The current position is ",""+position);
                  if(position&lt;6)
                  {
  
                  Log.e("The current position is ",""+position);
                  final YouTubeVideoRepeater youTubeVideoRepeater = youTubeVideoRepeaters.get(position);
                  ((Activity)itemView.getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                  int width = displayMetrics.widthPixels;
  
                  if(youTubeVideoRepeater.getTitle()!=null)
                      textWaveTitle.setText(youTubeVideoRepeater.getTitle());
                  else
                      textWaveTitle = null;
  
                  if(youTubeVideoRepeater.getImage_url()!=null)
                      Glide.with(itemView.getContext()).load(youTubeVideoRepeater.getImage_url()).apply(new RequestOptions().override(width-36,200)).into(imageViewItems);
                  else
                      Glide.with(imageViewItems).clear(imageViewItems);
  
                  imageViewItems.setVisibility(View.VISIBLE);
                  playButton.setVisibility(View.VISIBLE);
                  youTubePlayerView.setVisibility(View.GONE);
  
                  playButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          imageViewItems.setVisibility(View.GONE);
                          youTubePlayerView.setVisibility(View.VISIBLE);
                          playButton.setVisibility(View.GONE);
                          youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
                              @Override
                              public void onReady(YouTubePlayer youTubePlayer) {
                                  super.onReady(youTubePlayer);
                                  youTubePlayer.loadVideo(youTubeVideoRepeaters.get(position).getVideo_id(),0);
                              }
                          });
  
                      }
                  });
  
  
                  }
  
                  break;
          }
  
      }
  
  
      private class VIEW_TYPES {
  
          public static final int Header = 0;
          public static final int Footer = 7;
      }
  </code></pre>
  
  <p><strong>Update: Disabling the Headerview resolves the issue but I don't understand why it is happening.</strong> </p>
  
  <p>The Adpater for the <code>RecyclerView</code> in the header is as follows</p>
  
  <p><strong>TopScrollAdapter</strong></p>
  
  <pre><code> @NonNull
      @Override
      public TopScrollAdap.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view_item,parent,false);
          viewHolder = new ViewHolder(v);
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(@NonNull TopScrollAdap.ViewHolder holder, int position) {
          viewHolder.top_scroll_list_item.setText(top_scroll_items.get(position));
      }
  
  
      @Override
      public int getItemCount() {
          return top_scroll_items.size();
      }
  
  
      public class ViewHolder extends RecyclerView.ViewHolder
      {
  
          TextView top_scroll_list_item;
  
          public ViewHolder(@NonNull View itemView) {
              super(itemView);
              top_scroll_list_item = itemView.findViewById(R.id.top_scroll_list_item);
          }
  
      }
  </code></pre>