为什么listview的scrollTo(x,y)方法不会进入listview的末尾

时间:2014-08-13 12:23:59

标签: android android-listview

我有ListView Object并使用:

    Timer timer=new Timer();

    timer.schedule(new TimerTask() {
        @Override
        public void run() {
           runOnUiThread(new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
           lv1.scrollBy(0, counter_automatic);
           counter_automatic++;

                }
            });
        }
    }, 0, 100);

代码正常工作,不会出现任何错误!但是我希望Scrolling一直到listView的结尾!

我的listView有286 item!但它仅适用于3个项目,3个项目滚动后继续但项目don't appear

有什么问题?

4 个答案:

答案 0 :(得分:1)

scrollBy方法将像素作为参数。我想你混合了多少像素的物品。

如果您想平滑滚动到某个项目,请改用smoothScrollToPosition。见http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)

答案 1 :(得分:1)

scrollBy()方法是View类的成员,并且由于它处理其子ListView的原因而无法正常工作View 1}}秒。根据所需行为的具体情况,您可能希望使用smoothScrollToPosition(int position)smoothScrollBy(int distance, int duration)

答案 2 :(得分:0)

尝试使用setSelectionFromTop(int position,int pixelsFromTop),而不是使用scrollBy。在您的情况下,使用counter_automatic作为位置,并让pixelsFromTop变量不要更改。

答案 3 :(得分:0)

scrollBy()滚动一定数量的像素,而不是按项目滚动。 The documentation of scrollBy()说:

  

public void scrollBy(int x,int y)
   移动视图的滚动位置。这将导致调用onScrollChanged(int,int,int,   int)并且视图将失效。

     

<强>参数
  x - 水平滚动的像素数量
  y - 垂直滚动的像素数

所以你想要做的是这样的事情:

listView.scrollTo(0, listView.getHeight());

使用scrollTo()代替scrollBy()对您的用例应该更可靠。

但我想你实际想要的方法是smoothScrollToPosition()!尝试这样的事情:

listView.smoothScrollToPosition(adapter.getCount() - 1);