在android中禁用/启用ListView的滚动

时间:2014-03-21 12:27:04

标签: android listview android-listview scrollview

我在ListView内有一个ScrollView。我可以通过

启用ListView的滚动
listView.getParent().requestDisallowInterCeptTouchEvent(true);

但问题是当我在listView中向上滚动并且它到达顶部时它应该滚动到父视图,即父滚动必须工作。我怎样才能做到这一点 ?任何建议请。

3 个答案:

答案 0 :(得分:5)

listView.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            return true; // Indicates that this has been handled by you and will not be forwarded further.
        }
        return false;
    }
});

OR

要使视图无法选择,只需获取视图和.setClickable(false)

OR

listView.setScrollContainer(false);

答案 1 :(得分:4)

您可以覆盖ScrollView类并在其中插入这些方法:

private boolean isScrollEnabled = true;

public void enableScroll(boolean isScrollEnabled ) {
    this.isScrollEnabled = isScrollEnabled ;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (isScrollEnabled) {
        return super.onInterceptTouchEvent(ev);
    } else {
        return false;
    }
}

这是实现这一目标的最简洁的解决方案。您只需拨打scrollView.enableScroll(true)即可启用滚动功能,或scrollView.enableScroll(false)将其停用。

答案 2 :(得分:1)

我建议将您的上层视图,即列表视图上方的任何视图组嵌入到listview标题中。 ListView有一个方法listview.addHeaderView()。这样,即使在小尺寸显示器上也可以滚动列表(整个视图),并且您不需要滚动视图。

相关问题