【发布时间】:2017-04-17 01:17:18
【问题描述】:
我正在开发一个提要风格的应用程序,用户在可滚动的RecyclerView 中有很多帖子。我还有另一个Activity,用户访问它本质上是CalendarView,用户可以从中选择一个日期以在RecyclerView 中导航。稍后会详细介绍。
RecyclerView 中的每个项目都是一个Post。 Post 类如下所示:
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);
}
}
请注意,所有Posts 都按RecyclerView 内的日期排序。它们经过排序,以便最新的帖子位于顶部。
在我的MainActivity 内部,我有以下代码来设置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);
其中myRecyclerView 是RecyclerView,adapter 是RecyclerView.Adapter,layoutManager 是RecyclerView.LayoutManager,posts 是List<Post>。
假设在另一个日历Activity 中选择的日期不一定是实际发布的日期,但可以是任何日期。因此,如果我有 2017 年 1 月 1 日和 2017 年 3 月 15 日的帖子,并且我选择 2017 年 2 月 1 日,RecyclerView 会将 2017 年 3 月 15 日的帖子放在顶部,因为这是之后的第一个帖子日期。
来自this answer,我相信一旦我有了索引,我可以用layoutManager.scrollToPositionWithOffset(desiredIndex, 0);滚动到它,但是我没有测试过,我真的不知道如何找到desiredIndex
如果您需要任何其他信息或更多我的代码,请告诉我。
【问题讨论】:
标签: android date search android-recyclerview