Detecting the previous item in recycler view

android-recyclerview get item view at position
recyclerview get item at position
getting same item position in recyclerview when click on back button
how to get item id in recyclerview android
how to get position of recyclerview in android
get adapter position in activity

I have a model class called (messages). In on bind view holder method of recycler view adapter I do this to extract the (time) string from the (messages) class:

public void onBindViewHolder(final ViewHodler view holder, int position){

  //here I extract the time value from (messages) class

  messages mess=messageslist.get(position);

  //I store it in a long variable called time

  long time= mess.getTime();


Until here I am only getting the time value of each item in recycler view. But I want also to get the time of the previous item and compare it to the next one.

In other words I am trying to find the difference of time between each 2 items in recyclerview.

Thanks for your help.


okay what I want to do is to show or hide a date view if the difference of date between the 2 items reached or passed 1 day. If I do it according to the answers below I get no this

    //the time of the item
    long time= mess.getTime();

     //the time of the previous item according to the answers below

    long pre_time=messageslist.get(position-1).getTime();

     //now I compare the difference 

      if(time - pre_time >= 24*3600*1000){ 

         //show date
         //hide date

but doest work.

Edited the answer based on updated question

You should convert the time milliseconds to DAY_OF_MONTH and check if the day of the current item is greater than the day of the previous item.

Calendar calendar = Calendar.getInstance();

Messages mess = messagesList.get(position);
int date = calendar.get(Calendar.DAY_OF_MONTH);

if (position > 0) {
    Messages previousMessage = messagesList.get(position - 1);
    if (previousMessage != null) {
        int prevDate = calendar.get(Calendar.DAY_OF_MONTH);

        if (date > prevDate) {
            // This is a different day than the previous message, so show the Date
        } else {
            // Same day, so hide the Date

} else {
    // This is the first message, so show the date

Detecting the previous item in recycler view, Detecting the previous item in recycler view. android-recyclerview get item view at position recyclerview get item at position getting same item position in  Android RecyclerView Pagination – Next/Previous Pagination. In this class we see how to page/paginate data which is very important especially with RecyclerView which are meant to show large datasets.

messages mess = messageslist.get(position);
if (position > 0){
    long time_diffrence = mess.getTime() - messageslist.get(position-1).getTime();

Android: Detect when the RecyclerView reaches the bottom, Sometimes we need to detect weather a RecyclerView hits the bottom for Create an item view in your res/layout folder, i called it my_item.xml. Questions: I need to detect the start/end and direction of scroll in a recyclerview. The scroll listener has two methods: onScrolled() and onScrollStateChanged(). The first method is called after the scroll is started (indeed is called onScrolled() and not onScrolling()).

(1) Step one timestamp to Date convert menthod

private String getDate(String createdDate) {
    long timestampString = Long.parseLong(createdDate);
    @SuppressLint("SimpleDateFormat") String value = new java.text.SimpleDateFormat("dd/MM/yyyy").
            format(new java.util.Date(timestampString * 1000));
    return value;

(2) Step two in onBindViewHolder

    if (position == 0) {
    } else {
        String prev_date_time = getDate(mPojoTicketChatList.get(position - 1).getCreatedDate());
        //String prev_date[] = prev_date_time[0].split("-");
        if (getDate(bean.getCreatedDate()).equals(prev_date_time)) {
        } else {

Try it working for me

Using the RecyclerView, Manual Click Detection - ListView has a AdapterView. A layout manager positions item views inside a RecyclerView and determines when to reuse item views  The first item gets selected using a long click. Any subsequent click will select or deselect items. Users can also deselect items, that were previously selected, with a single click. The user can then use a bulk delete operation on all selected items.

Detecting the previous item in recycler view, getTime(); } Until here I am only getting the time value of each item in recycler view. But I want also to get the time of the previous item and compare it to the next  The most obvious way to detect items perceived by the user is to check the scroll state and mark your list items “viewed”. In this way, you will need to add a timestamp to every item. This timestamp should be set when item comes to the viewport.

Detecting List Items Observed by User, It makes your RecyclerView look and feel like a good old ListView. To achieve the basic list item visibility detection, we can go with these two  Recyclerview and cardview dependency. Add recyclerview in activity_main.xml where you removed the textview as shown below. < android:layout_width="match

Create a List with RecyclerView, Using RecyclerView to display lists and grids of dynamic content. Detecting location · Requesting Permissions · Using the Speaker Each view holder is in charge of displaying a single item with a view. When the selection becomes empty (if the user cleared the selection the last time), don't forget to terminate action  class MyAdapter(private val myDataset: Array<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() { // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder.

  • Maybe if(position > 0) { ... messageslist.get(position - 1); } ?
  • how are you setting the time ?
  • @SumitAnantwar I used date format to convert the long into date as day/month/year....the problem is just when to show the date view which is just a TextView.
  • @SumitAnantwar my problem is not by converting the millisecond long to date problem is in showing the view when the difference of time between 2 messages pass the day. So that users know which group of messages correspond to what date.
  • (time - pre_time >= 24*3600*1000) will check if there is a difference of 24 hours between the two consecutive messages. This would be true only if the user has not sent any message for more than 24 hours.
  • I want to know when to show a date above a new message group...any thoughts? thanks.
  • What are your criteria for showing the date? If you observe chat apps like Slack or Skype, as per my observation they generally group the messages per 5 mins interval.
  • I am trying to do like most common as per day.
  • If you are grouping per day, then you have to just check if the Date of the current item is greater than the date of the previous item, if true display the date. Please correct me if I did not understand your use case.
  • @Sumit if a user adds message every day, consider this scenario, if date = 2nd of month and for previous days messages prevDate = 1st then when date = 1st of a month and for previous days messages prevDate = 31st or 30th of previous month, then the condition if (date > prevDate) {} fails as 1 > 31 is false. You might want to use if(date != prevDate)
  • I have a question, if I want to check if the time between the 2 messages to know where to show the date above the message that states that a new chat group happend ....then how does your code help? thanks.