Android的ListView的项目添加到上面没有列表视图滚动
我有一个ListView,我想新的项目添加到列表视图的顶部,但我不想列表视图滚动的内容。我希望用户能够查看相同的项目,因为他是新的项目加入看着面前。
I have a listView and I want to add new items to the top of the list view but I dont want list view to scroll its content. I want user to look at the same item as he was looking before new items were added.
这是我如何增加新项目的ListView:
This is how I add new items to ListView:
this.commentsListViewAdapter.addRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();
和这是 addRangeToTop
方法:
public void addRangeToTop(ArrayList<Comment> comments)
{
for (Comment comment : comments)
{
this.insert(comment, 0);
}
}
这是我的ListView:
this is my listView:
<ListView
android:id="@+id/CommentsListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/AddCommentLayout"
android:stackFromBottom="true" >
</ListView>
我想要做的是加载老的意见,当用户滚动到顶部。
What I want to do is to load old comments when user scrolls to the top.
感谢您的帮助。
我已经找到解决方案在这里Retaining在调用后,ListView控件位置notifyDataSetChanged
I have found solution here Retaining position in ListView after calling notifyDataSetChanged
抱歉重复的问题。 最后code是这样的:
Sorry for duplicate question. The final code is this:
int index = this.commentsListView.getFirstVisiblePosition() + comments.size();
View v = this.commentsListView.getChildAt(commentsListView.getHeaderViewsCount());
int top = (v == null) ? 0 : v.getTop();
this.commentsListViewAdapter.AddRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();
this.commentsListView.setSelectionFromTop(index, top);