在RecyclerView的顶部添加新项目 [英] Adding new item to the top of the RecyclerView

查看:249
本文介绍了在RecyclerView的顶部添加新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式将一个项目添加到回收站视图位置0

I am adding an item to recyclerview position 0 programamticly

public void addQuestion(Question question){
    this.questionList.add(0, question);
    notifyItemInserted(0);
}

此方法运行良好,但确实出现在顶部列表中用户必须向上滚动才能看到新项目。

This is working very well and the items do appear in the list at top BUT the user has to scroll up to see the new item.

是否有任何技巧可以使项目显示在顶部,并且recyclerview会自动向上滚动?

Is there any trick how the item appear at top and recyclerview is scrolling up automaticly ?

推荐答案

那么您可以使用 mRecyclerView.smoothScrollToPosition(int position)

示例:

public void addQuestion(Question question){
    this.questionList.add(0, question);
    notifyItemInserted(0);
    mRecyclerView.smoothScrollToPosition(0);
}

更新:

如果您想真正平滑地滚动到某个项目,可以查看此问题的答案

if you want to make the scrolling to certain item really smooth you can have a look at answer to this question

RecyclerView-如何可以平滑滚动到某个位置的项目顶部?

这篇关于在RecyclerView的顶部添加新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆