I am working on a feed-style app, where a user has a lot of posts in a scrollable RecyclerView
. I have another Activity
that the user goes to that is essentially a CalendarView
, and the user can select a date from it to navigate to in the RecyclerView
. More on this later.
Each item in the RecyclerView
is a Post
. The Post
class looks like this:
public abstract class Post implements Comparable<Post> {
public String userId;
public Date date;
public Post() {
}
protected Post(String userId, Date date) {
this.userId = userId;
this.date = date;
}
@Override
public int compareTo(@NonNull Post o) {
return date.compareTo(o.date);
}
}
Note that all of the Post
s are sorted by their dates inside of the RecyclerView
. They are sorted so that the most recent posts are at the top.
Inside of my MainActivity
, I have the following code to set up the RecyclerView
:
myRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
myRecyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(this);
myRecyclerView.setLayoutManager(layoutManager);
adapter = new SocialJournalAdapter(posts, this);
myRecyclerView.setAdapter(adapter);
Where myRecyclerView
is a RecyclerView
, adapter
is a RecyclerView.Adapter
, layoutManager
is a RecyclerView.LayoutManager
, and posts
is a List<Post>
.
Assume that the date that is chosen inside of the other calendar Activity
isn't necessarily the date of an actual post, but could be any date. So, if I have posts dated Jan 1, 2017 and Mar 15, 2017, and I choose February 1, 2017, the RecyclerView
will put the post dated Mar 15, 2017 at the top, since it is the first post after that date.
From this answer, I believe that once I have the index, I can scroll to it with layoutManager.scrollToPositionWithOffset(desiredIndex, 0);
, but I haven't tested this out, and I don't really know how to find the desiredIndex
Let me know if you need any other information or more of my code.