禁用列表视图中的滚动
我有一个列表视图,根据某些逻辑我想暂时禁用滚动。 view.setOnScrollListener(null);对我没有帮助,我想我需要编写一些代码,有人可以给我一个历史记录或一些片段吗?
谢谢
I have a list view and depending on some logic I want to temporary disable the scrolling.
view.setOnScrollListener(null); doesn't helps me I guess I need to write some code, can someone give me a hist or some snippet ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在您的 CustomListView 中:
然后 ListView 将对点击做出反应,但不会更改滚动位置。
In your CustomListView:
Then ListView will react to clicks, but will not change scroll position.
无需创建新的自定义 ListView 的另一种选择是将
onTouchListener
附加到 ListView,并在onTouch()
回调中返回 true(如果运动事件操作为ACTION_MOVE)
。Another option without creating a new custom ListView would be to attach an
onTouchListener
to your ListView and return true in theonTouch()
callback if the motion event action isACTION_MOVE
.使用 listview.setEnabled(false) 禁用列表视图滚动
注意:这也会禁用行选择
use listview.setEnabled(false) to disable listview scrolling
Note: This also disables row selections
将您的
CustomListView
设置为
false
,子级将处理触摸事件,请确保您设置了if 条件
来检查是否需要滚动。make your
CustomListView
on
false
the childs will handle the touch event, make sure you put yourif condition
to check you need to scroll or not.如果您有一个事件绑定到列表项,则使用任何这些解决方案拖动列表仍将触发该事件。您希望使用以下方法来满足用户通过拖动所选项目来取消事件的期望(改编自 Pointer Null 的答案):
可以使用完整的自定义视图类: https://gist.github.com/danosipov/6498490
If you have an event bound to the list items, then dragging the list with any of these solutions will still trigger the event. You want to use the following method to account for users expectation to cancel the event by dragging away from the selected item (adapted from Pointer Null's answer):
Full custom view class is available: https://gist.github.com/danosipov/6498490
对我来说最好的答案是 Dan Osipov 的答案。
我会像这样改进它。 (触发取消操作事件而不是手动擦除按下状态)。
The best answer for me is the One from Dan Osipov.
I'd improve it like this. (firing a CANCEL action event instead of manually erasing the pressed state).
在编写用于在列表视图上滑动删除的代码时,我想在检测到滑动后阻止垂直滚动。一旦满足滑动删除条件,我就会在 ACTION_MOVE 部分设置一个布尔标志。 dispatchTouchEvent 方法检查该条件并阻止滚动工作。在 ACTION_UP 中,我将标志设置回 false 以重新启用滚动。
When writing code for swipe to delete on a list view, I wanted to prevent the vertical scrolling once the swipe had been detected. I set a boolean flag in the ACTION_MOVE section once the swipe to delete conditions had been met. The dispatchTouchEvent method checks that condition and prevents the scroll from working. In the ACTION_UP I set the flag back to false to re-enable the scroll.
我的回答对于 Xamarin 和 MvvmCross 用户来说会很有趣。主要概念与之前的文章相同,因此主要步骤是:
这里您是帮助器类,它允许在列表视图中禁用滚动:
My answer will be interesting for Xamarin and MvvmCross users. Main concept is the same like in previous posts, so main steps are:
Here you are helper class, that allows to disable scroll in list view:
这是 Joe Blow(对 OP 帖子的评论)在 http://danosipov.com/?p= 上指出的代码604 但我将其发布在这里是为了保留它,以防链接被孤立:
当您将此 ListView 添加到布局中时,请记住在其前面加上其包名称,否则当您尝试膨胀时将引发异常它。
This is the code Joe Blow (comment on OP post) pointed at on http://danosipov.com/?p=604 but I'm posting it here to preserve it in case the link is orphaned:
When you add this ListView to your layout remember to precede it with its package name, otherwise an exception will be thrown when you try to inflate it.
试试这个:
对我有用。
Try this:
Works for me.
将 listView 放入nestedScrollView 中,并使用以下选项之一禁用滚动:
注意:使用以下选项之一就足够了。
注意:只有将 listView 放入nestedScrollView 中时,此方法才有效。如果您不希望 listView 位于nestedScrollView 内并且仍想禁用滚动,那么您需要按照接受的答案中的建议创建 CustomListView 。
Put your listView inside nestedScrollView and disable scroll using one of the following options:
Note: using one of the following option is enough.
Note: this works only if listView is put inside nestedScrollView. If you don't want your listView located inside nestedScrollView and still want disable scroll, then you need to create CustomListView as suggested in accepted answer.