firstVisibleItem、visibleItemCount、totalItemCount 即使发生 Scroll 事件也不会改变

firstVisibleItem, visibleItemCount, totalItemCount do not change even Scroll event happened

我有一个包含Listview的ScrollView,但是即使我滚动listview,onscroll()方法中的三个参数也没有改变,它们仍然与初始状态相同。

这是我的布局

还有我处理滚动事件的代码,注意 contentlist 是列表视图而不是滚动视图。

然而,正如您在 logcat 中看到的那样,即使我四处滚动也没有任何变化,可见项目数应该只有 两个

不知道是不是listview的高度问题导致的?

谢谢

ScrollView 中包含 ListView 总是会导致问题。为什么不在 SwipeRefreshLayout 中包含 ListView 并使用 addHeaderView() 在上面显示你的 LinearLayout?

首先,我不明白你为什么要在 ScrollView 中保留一个 ListView。

您不应该将 ListView 放在 ScrollView 中,因为 ListView class 实现了它自己的滚动,它只是不接收手势,因为它们都由父 ScrollView 处理。

但是如果你仍然需要 ScrollView 里面的 ListView ,你可以像这样给 ListView 添加触摸监听器并禁用父 ScrollView 的触摸事件..

contentList.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // Disallow the touch request for parent scroll on touch of child view
    v.getParent().requestDisallowInterceptTouchEvent(true);
    return false;
    }
});

希望对您有所帮助..:)