Android ListView 在没有列表视图滚动的情况下将项目添加到顶部 [英] Android ListView add items to top without list view scroll

查看:28
本文介绍了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
    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.

感谢您的帮助.

推荐答案

我在这里找到了解决方案 调用notifyDataSetChanged后在ListView中保留位置

I have found solution here Retaining position in ListView after calling notifyDataSetChanged

抱歉重复提问.最后的代码是这样的:

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);

这篇关于Android ListView 在没有列表视图滚动的情况下将项目添加到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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