以编程方式在ListView中设置位置,而无需滚动 [英] Programmatically set position in ListView without scrolling

查看:79
本文介绍了以编程方式在ListView中设置位置,而无需滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对运行Android 4.4.2的平板电脑.我有一个包含数百个项目的ListView,一次约有20个可见.用户不需要平滑滚动的动画.如何在不使用 smoothScrollToPosition()的情况下以编程方式设置Listview中的显示位置?

This is for a tablet running Android 4.4.2. I have a ListView with hundreds of items in it and about 20 are visible at a time. The user doesn't want the animation of smooth scrolling. How do I programmatically set the displayed position in a Listview without using smoothScrollToPosition()?

我搜索了堆栈溢出,并且在 Android ListView setSelection()中没有找到似乎起作用,他们建议这样做:

I searched Stack Overflow and in Android ListView setSelection() does not seem to work they suggested this:

mListView.clearFocus();
mListView.post(new Runnable() {
    @Override
    public void run() {
        mListView.setSelection(index);
    }
});

...但是它只是设置选择;它不会使ListView的该部分可见. setSelection()似乎是整个网络上的一种流行解决方案,但我在文档中找不到任何内容来说明 setSelection()也可以设置位置,并且只能设置选择并不会改变我的立场.

. . . but it just sets the selection; it does not bring that portion of the ListView into view. setSelection() seems like a popular solution all over the web but I couldn't find anything in the documentation saying that setSelection() also sets the position, and it ONLY sets the selection and does not change the position on mine.

中,无需使用smoothScrollToPosition即可转到Listview中的项目他们提出了罗曼·盖伊(Romain Guy)的解决方案...

In Go to a item in Listview without using smoothScrollToPosition they suggested a solution by Romain Guy ...

[myListView.post(new Runnable() 
{
    @Override
    public void run() 
    {
        myListView.setSelection(pos);
        View v = myListView.getChildAt(pos);
        if (v != null) 
        {
            v.requestFocus();
        }
    }
});] 

这个问题是我的ListView是ListActivity的一部分,该ListActivity通过自定义适配器的 getView()进行管理,因此不可见的视图将被回收,即,如果我请求一个孩子屏幕上未显示的视图的视图返回 null .无论如何,它实际上是我要控制的ListView,因此通过子级View间接执行它似乎完全是间接的.

The problem with this one is that my ListView is part of a ListActivity being managed via a custom adapter's getView(), so Views that are not visible are recycled, i.e., if I request a child view of a view that's not on the screen it returns null. Anyway, it's really the ListView I'm trying to control, so doing it indirectly via a child View seems awfully indirect.

如何告诉ListView我想在屏幕上看到哪一部分?

How do I tell the ListView what part of it I want visible on the screen?

推荐答案

AbsListView中有一个名为 smoothScrollToPositionFromTop()的方法,该方法需要 duration 参数.因此,如果将其设置为0,则无需滚动动画即可完成操作.

There is a method in AbsListView, called smoothScrollToPositionFromTop() and it takes duration parameter. So if you set it to 0, you may do it without scrolling, animation.

smoothScrollToPositionFromTop

这篇关于以编程方式在ListView中设置位置,而无需滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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