title: How can I make sticky headers in RecyclerView? (with or without external lib)
body: <p>I'm using room db and I've a table from where I get a list of LiveData.
  In that table there is a column of Date where I store current date. Current date is selected by default, but user can also change the date when inserting data in the database. </p>
  
  <p>I want to show this data in a recyclerview in this manner 
  <a href="https://i.stack.imgur.com/pzV16.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pzV16.jpg" alt="Image"></a>
  <a href="https://imgur.com/RYegFpG" rel="nofollow noreferrer">https://imgur.com/RYegFpG</a></p>
  
  <p>I want to section this data according to the month and year as header and all entries of that month year below it.</p>
  
  <p>For example, user inserted data in October 2019, I want this "October 2019" as a header in recyclerview and all entries of this month below it. Just like this all months entries should be shown with same manner as every next month becomes header and the entries of that month below it. </p>
  
  <p>I've tried to achieve this by doing  </p>
  
  <pre><code>if (!thisDate.equals(dBDate))
          {
              holder.transMonthWrapper.setVisibility(View.VISIBLE);
  
              if (IEList.getType().equalsIgnoreCase("income"))
              {
                  String amount = ""+IEList.getAmount();
                  holder.tvTransAmount.setText(amount);
                  holder.tvTransAmount.setTextColor(Color.GREEN);
  
                  holder.tvTransCategory.setText(IEList.getCategory());
                  holder.tvTransCategory.setTextColor(Color.GREEN);
              }
              else
              {
                  String amount = ""+IEList.getAmount();
                  holder.tvTransAmount.setText(amount);
                  holder.tvTransAmount.setTextColor(Color.RED);
  
                  holder.tvTransCategory.setText(IEList.getCategory());
                  holder.tvTransCategory.setTextColor(Color.RED);
              }
  
              thisDate = dBDate;
  
              holder.tvTransMonth.setText(thisDate);
          }
  
          else
          {
              holder.transMonthWrapper.setVisibility(View.GONE);
  
              if (IEList.getType().equalsIgnoreCase("income"))
              {
                  String amount = ""+IEList.getAmount();
                  holder.tvTransAmount.setText(amount);
                  holder.tvTransAmount.setTextColor(Color.GREEN);
  
                  holder.tvTransCategory.setText(IEList.getCategory());
                  holder.tvTransCategory.setTextColor(Color.GREEN);
              }
              else
              {
                  String amount = ""+IEList.getAmount();
                  holder.tvTransAmount.setText(amount);
                  holder.tvTransAmount.setTextColor(Color.RED);
  
                  holder.tvTransCategory.setText(IEList.getCategory());
                  holder.tvTransCategory.setTextColor(Color.RED);
              }
          }
  </code></pre>
  
  <p>But the problem in this code is that when user change the month from settings and put some entries into database and that year's month entries are already present in the recyclerview. It creates another header of that existing month in recyclerview. But I want this to put those entries in existing month header, not to create new header of that month.</p>
  
  <p>What will be the best approach to achieve this without using external libraries, because I don't want to be dependent on external libraries in this case. </p>
  
  <p>I'm fairly new in programming.</p>
  
  <p><strong>Updated</strong></p>
  
  <p>In activity</p>
  
  <pre><code> public void getTransactionData()
  {
      adapter = new TransactionAdapter();
      recyclerView.setAdapter(adapter);
  
      incomeExpenseModel = ViewModelProviders.of(AllTransaction.this).get(IncomeExpenseViewModel.class);
      incomeExpenseModel.getIncomeExpenseData().observe(this, new Observer&lt;List&lt;IncomeExpense&gt;&gt;() {
          @Override
          public void onChanged(List&lt;IncomeExpense&gt; incomeExpenses) {
  
              adapter.setIncomeExpenseList(incomeExpenses);
          }
      });
  </code></pre>
  
  <p>In recyclerAdapter</p>
  
  <pre><code>public void onBindViewHolder(@NonNull TransactionViewHolder holder, int position) {
  
      IncomeExpense IEList = incomeExpenseList.get(position);
  
      preferences = context.getSharedPreferences(settingPref, Context.MODE_PRIVATE);
      String dateFormat = preferences.getString("Date_Format", "MM.dd.yy");
  
      int lastIndex = incomeExpenseList.size() - 1;
      IncomeExpense IELastIndex = incomeExpenseList.get(lastIndex);
  
      String dateFrmDb= IELastIndex.getDate();
      DateFormat df=new SimpleDateFormat(dateFormat);
      Date d;
  
      try {
          d = df.parse(dateFrmDb);
          df=new SimpleDateFormat("MMMM yyyy");
          if (d != null) {
              dBDate = df.format(d);
          }
      } catch (ParseException e) {
  
          Toast.makeText(context, "Error" +e, Toast.LENGTH_SHORT).show();
      }
  
      if (!thisDate.equals(dBDate))
      {
          holder.transMonthWrapper.setVisibility(View.VISIBLE);
  
          if (IEList.getType().equalsIgnoreCase("income"))
          {
              String amount = ""+IEList.getAmount();
              holder.tvTransAmount.setText(amount);
              holder.tvTransAmount.setTextColor(Color.GREEN);
  
              holder.tvTransCategory.setText(IEList.getCategory());
              holder.tvTransCategory.setTextColor(Color.GREEN);
          }
          else
          {
              String amount = ""+IEList.getAmount();
              holder.tvTransAmount.setText(amount);
              holder.tvTransAmount.setTextColor(Color.RED);
  
              holder.tvTransCategory.setText(IEList.getCategory());
              holder.tvTransCategory.setTextColor(Color.RED);
          }
  
          thisDate = dBDate;
  
          holder.tvTransMonth.setText(thisDate);
      }
  
      else
      {
          holder.transMonthWrapper.setVisibility(View.GONE);
  
          if (IEList.getType().equalsIgnoreCase("income"))
          {
              String amount = ""+IEList.getAmount();
              holder.tvTransAmount.setText(amount);
              holder.tvTransAmount.setTextColor(Color.GREEN);
  
              holder.tvTransCategory.setText(IEList.getCategory());
              holder.tvTransCategory.setTextColor(Color.GREEN);
          }
          else
          {
              String amount = ""+IEList.getAmount();
              holder.tvTransAmount.setText(amount);
              holder.tvTransAmount.setTextColor(Color.RED);
  
              holder.tvTransCategory.setText(IEList.getCategory());
              holder.tvTransCategory.setTextColor(Color.RED);
          }
      }
  
  }
  
  @Override
  public int getItemCount() {
      return incomeExpenseList.size();
  }
  
  public void setIncomeExpenseList(List&lt;IncomeExpense&gt; incomeExpenseList)
  {
      this.incomeExpenseList = incomeExpenseList;
      notifyDataSetChanged();
  }
  </code></pre>