机器人:smoothScrollToPosition()无法正常工作 [英] android: smoothScrollToPosition() not working correctly

查看:3950
本文介绍了机器人:smoothScrollToPosition()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加一个元素与ListView关联一个ArrayAdapter后顺利滚动到列表的最后一个元素。 的问题是,它只是滚动到一随机位置

  arrayadapter.add(项目);
//不能正常工作:
listview.smoothScrollToPosition(arrayadapter.getCount() -  1);

//工作得很好:
listview.setSelection(arrayadapter.getCount() -  1);
 

解决方案

您可能想告诉ListView控件后滚动时,UI线程可以处理它(这就是为什么你难道不是滚动正确)。 SmoothScroll需要做大量的工作,而不是只去一个位置,无视速度/时间/等。 (需要一个动画)。

因此​​,你应该做的是这样的:

  getListView()后(新的Runnable(){
        @覆盖
        公共无效的run(){
            。getListView()smoothScrollToPosition(POS);
        }
    });
 

I'm trying to smoothly scroll to last element of a list after adding an element to the arrayadapter associated with the listview. The problem is that it just scrolls to a random position

arrayadapter.add(item);
//DOES NOT WORK CORRECTLY:
listview.smoothScrollToPosition(arrayadapter.getCount()-1);

//WORKS JUST FINE:
listview.setSelection(arrayadapter.getCount()-1);

解决方案

You probably want to tell the ListView to post the scroll when the UI thread can handle it (which is why yours it not scrolling properly). SmoothScroll needs to do a lot of work, as opposed to just go to a position ignoring velocity/time/etc. (required for an "animation").

Therefore you should do something like:

    getListView().post(new Runnable() {
        @Override
        public void run() {
            getListView().smoothScrollToPosition(pos);
        }
    });

这篇关于机器人:smoothScrollToPosition()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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