Android的ListView控件 - 滚动回在更新前 [英] Android ListView - scrolls back to top on update

查看:135
本文介绍了Android的ListView控件 - 滚动回在更新前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,获取额外的要求补充意见,这是一个BaseAdapter维护。我怎样才能保持更新后滚动位置?

I have a listview that gets additional views added on request, that's maintained by a BaseAdapter. How can I maintain scroll position after the update?

我知道这已经被问了几次,但每次,相同的解决方案已被提出来,我都试过了,这是调用 adapter.notifyDataSetChanged(); 更新包含列表内容的ArrayList后。

I know this has been asked a few times, but each time, the same solution has been put forward, which I have tried, which is to call adapter.notifyDataSetChanged(); after updating the ArrayList that contains the list contents.

我如何确保滚动位置得住?

How can I ensure that scroll position is maintained?

推荐答案

实现您的活动类的OnScrollListener,然后使用下面的code:

Implement an OnScrollListener in your activity class and then use the following code:

int currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount;
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
    this.currentTotalItemCount = totalItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
}

private void isScrollCompleted() {

    if (currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount) {
        if (this.currentVisibleItemCount > 0
                && this.currentScrollState == SCROLL_STATE_IDLE) {

            //Do your work
        }
    }
}

如果您使用的是AsyncTask的更新数据,那么你就可以在您的PostExecute以下,以保持滚动位置:

If you are using AsyncTask for updating your data, then you can include the following in your PostExecute in order to maintain the Scroll position:

list.setAdapter(adapter);
list.setSelectionFromTop(currentFirstVisibleItem, 0);

我希望这有助于。

I hope this helps.

这篇关于Android的ListView控件 - 滚动回在更新前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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