title: I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works
body: <p>I have an app trying to show its items from the latest item added to earlier ones in a grid view with GridLayoutManager, getting the items from firebase database, setReverseLayout works but when the items are not uniform or items total is an odd number, it leaves a vacant space for on item at the top like it omitted an item but it didnt, i tried using collections.reverse but it didnt really arrange the items from latest items, more like shuffled it and i still have that gap problem at the top. There is no error message, the app runs fine but its not displaying items the desired way.</p>
  <p>Here is a screenshot of the problem with both methods I tried</p>
  <p><img src="https://i.stack.imgur.com/knBF4m.jpg" alt="Screenshot" /></p>
  <p>Here is my recycler adapter code</p>
  <pre><code>package com.example.twelve.Adapters;
  
  import android.content.Context;
  import android.content.Intent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.example.twelve.HomeActivity;
  import com.example.twelve.Model.HomeProducts;
  import com.example.twelve.Products_Page_Activity;
  import com.example.twelve.R;
  import com.squareup.picasso.Picasso;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class RecyclerAdapter extends RecyclerView.Adapter&lt;RecyclerAdapter.ViewHolder&gt; {
      List&lt;HomeProducts&gt; lstproducts = new ArrayList&lt;&gt;();
      Context ncontext;
  
      public RecyclerAdapter(List&lt;HomeProducts&gt; lstproducts, Context ncontext) {
          this.lstproducts = lstproducts;
          this.ncontext = ncontext;
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          return new ViewHolder(LayoutInflater.from(ncontext).inflate(R.layout.home_product_items, parent, false));
      }
  
      @Override
      public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
          holder.cardview_pname.setText(lstproducts.get(position).getPname());
          holder.cardview_category.setText(lstproducts.get(position).getCategory());
          holder.cardview_price.setText(lstproducts.get(position).getPrice());
          Picasso.get().load(lstproducts.get(position).getImageurllist().get(0)).into(holder.cardview_image);
  
          holder.itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(view.getContext(), Products_Page_Activity.class);
                  intent.putExtra(&quot;pid&quot;, lstproducts.get(position).getPid());
                  intent.putExtra(&quot;prev_activity&quot;, &quot;Home&quot;);
                  view.getContext().startActivity(intent);
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return lstproducts.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder {
          public ImageView cardview_image;
          public TextView cardview_pname, cardview_category, cardview_price;
  
          public ViewHolder(@NonNull View itemView) {
              super(itemView);
              cardview_image = itemView.findViewById(R.id.rec_imgview);
              cardview_pname = itemView.findViewById(R.id.rec_textviewname);
              cardview_category = itemView.findViewById(R.id.rec_textviewcategory);
              cardview_price = itemView.findViewById(R.id.rec_textprice);
  
          }
      }
  }
  
  </code></pre>
  <p>Code to add products to the adapter</p>
  <pre><code>//Products are added in a list and passed to the adapter
          DatabaseReference pref = FirebaseDatabase.getInstance().getReference(&quot;HomeProducts&quot;);
          pref.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot snapshot) {
                  for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                      HomeProducts Hproducts = dataSnapshot.getValue(HomeProducts.class);
                      listtems.add(Hproducts);
                      Collections.reverse(listtems);
                  }
                  rcyAdapter = new RecyclerAdapter(listtems, HomeActivity.this);
                  rcyAdapter.notifyDataSetChanged();
                  recyclerView.setAdapter(rcyAdapter);
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError error) {
  
              }
          });
  </code></pre>