滚动一个ListView的位置光标加载后 [英] Scrolling a ListView to a position after cursor loads

查看:256
本文介绍了滚动一个ListView的位置光标加载后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView一个片段,我想在ListView保持其滚动位置时,片段保存和恢复。通常这是很容易,只需要调用ListView.getFirstVisiblePosition()保存和当ListView.setSelection()(ListView.setSelectionFromTop()如果你想获得花式),还原时。

I have a fragment with a ListView, and I want the ListView to maintain its scroll position when the fragment is saved and restored. Usually this is easy, just call ListView.getFirstVisiblePosition() when saving and ListView.setSelection() (ListView.setSelectionFromTop() if you want to get fancy) when restoring.

不过,我使用的装载机,和我的ListView不完全填充的活动开始时。如果我setSelection()onActivityCreated在()的名单将不会有任何滚动到着呢,通话将被忽略。

However, I'm using loaders, and my ListView isn't fully populated when the activity starts. If I setSelection() during onActivityCreated() the list won't have anything to scroll to yet, and the call will be ignored.

我得到解决此现在发布的滚动用于在将来某个时候,但是这绝对是不理想的。我想preFER为它向右滚动的数据完成加载。我几乎做的,在这里,但swapCursor()不刷新,它只是时间表的无效的未来。

I'm getting around this now by posting the scroll for sometime in the future, but this definitely isn't ideal. I'd prefer for it to scroll right as the data finishes loading. I'm almost doing that here, but swapCursor() doesn't refresh, it just schedules an invalidation for the future.

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
    if (savedScrollPosition != null) {
        Log.d(TAG, "loaded and scrolling to " + savedScrollPosition);
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                list.setSelection(savedScrollPosition);
                savedScrollPosition = null;
            }
        }, 100);
    }
}

你知道有什么方法滚动ListView的权利,ListView控件完成与数据填充的?

Do you know of any way of scrolling the ListView right as the ListView finishes populating with data?

推荐答案

试试这个:

listView.post(new Runnable() {

        @Override
        public void run() {
            listView.setSelection(savedScrollPosition);
            savedScrollPosition = null;
        }
    });

这篇关于滚动一个ListView的位置光标加载后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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