【问题标题】:Scroll list view manually in pixel (by smoothScrollByOffset)以像素为单位手动滚动列表视图(通过平滑滚动偏移)
【发布时间】:2014-06-02 20:11:39
【问题描述】:

我想当用户滚动列表视图 1 时,列表视图 2 也像列表视图 1 一样滚动(完全以像素为单位)。
我尝试通过 smoothScrollByOffset(offset) 滚动列表视图 2,
但它滚动的次数超过了列表视图 1。
不知道为什么?
smoothScrollByOffset(offset) 发生了什么或者我错过了什么?

更多信息:
滚动列表视图 2 的代码(在列表视图 1 的 onScroll 回调中):

    int previsous = 0;
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        //get current position in pixel of listview 1 
        View c = lv1.getChildAt(0);
        int scrolly = -c.getTop() + lv1.getFirstVisiblePosition() * c.getHeight();
        //Difference in pixel with the last scroll
        int delta = scrolly - previsous;
        Log.d("scroll", "listview 1 has scrolled: " + 
                scrolly + "| Difference with the last " + delta);

        //scroll listview 2 by delta
        lv2.smoothScrollByOffset(delta);
        previsous = scrolly;
    }

诊断截图:

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    尝试使用smoothScrollToPosition (int position) 而不是smoothScrollByOffset (int offset),希望对您有所帮助,让我知道发生了什么。

    【讨论】:

    • smoothScrollToPosition (int position) 在我的情况下不舒服。因为它是项目的位置,所以它按项目而不是按像素滚动。因此,当用户滚动时,列表视图 1 和 2 的项目中断行会有所不同(我希望这些项目中断行必须是同一行)。尽管有 2 个列表视图,但您可以对其进行成像,但在用户看来,这些必须是 1 个列表视图。
    • 好的!你能看看this定时器的答案吗?
    • 也许我让你对使用“更快”感到困惑,这与速度有关。因此,您建议使用计时器来控制滚动速度的解决方案。问题是 listview 2 比 listview 1 滚动更多(smoothScrollByOffset(offset) 不能按我的预期工作)。
    【解决方案2】:

    您使用了错误的方法。 smoothScrollByOffset() 不接受像素,而是位置数。使用smoothScrollBy(int distance, int duration)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2020-08-30
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多