2017-04-17 82 views
0

我正在使用一个Feed风格的应用程序,其中用户在可滚动的RecyclerView中有很多帖子。我还有另一个Activity,用户转到本质上是CalendarView,用户可以从中选择一个日期导航到RecyclerView。稍后再说。如何根据其中一项特性滚动到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); 
    } 
} 

注意,所有的Post S的被他们的日期排序的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); 

myRecyclerViewRecyclerViewadapterRecyclerView.AdapterlayoutManagerRecyclerView.LayoutManager,并postsList<Post>

假设在其他日历Activity内选择的日期不一定是实际文章的日期,但可以是任何日期。因此,如果我的帖子日期为2017年1月1日和2017年3月15日,并且我选择2017年2月1日,RecyclerView会将日期为2017年3月15日的帖子置顶,因为它是该日期之后的第一个帖子。

this answer,我相信,一旦我有指标,我可以滚动它与layoutManager.scrollToPositionWithOffset(desiredIndex, 0);,但我还没有测试了这一点,我真的不知道如何找到desiredIndex

设我知道你是否需要任何其他信息或更多我的代码。

回答

0

你可能想scrollToPosition(desiredIndex)

要找到所需的索引,通过您的“帖子”看,因为这是该RecyclerView显示的数据。

相关问题