禁用在列表视图中滚动

我有一个列表视图,并根据我想临时禁用滚动的一些逻辑。 view.setOnScrollListener(NULL); 不帮助我我想我需要写一些代码,有人可以给我一个hist或一些片段?

谢谢

在你的CustomListView中:

@Override public boolean dispatchTouchEvent(MotionEvent ev){ if(ev.getAction()==MotionEvent.ACTION_MOVE) return true; return super.dispatchTouchEvent(ev); } 

然后ListView会对点击作出反应,但不会改变滚动位置。

如果动作事件操作是ACTION_MOVE则不创build新的自定义ListView的另一个select是将onTouchListener附加到ListView,并在onTouch()callback中返回true。

 listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return (event.getAction() == MotionEvent.ACTION_MOVE); } }); 

使用listview.setEnabled(false)禁用listview滚动

注意:这也禁用行select

如果您有一个绑定到列表项的事件,那么使用这些解决scheme中的任何一个拖动列表仍将触发该事件。 您希望使用以下方法来解决用户期望通过从选定项目(从Pointer Null的答案改编而来)中拖动离开来取消事件:

 @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK; if (actionMasked == MotionEvent.ACTION_DOWN) { // Record the position the list the touch landed on mPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); return super.dispatchTouchEvent(ev); } if (actionMasked == MotionEvent.ACTION_MOVE) { // Ignore move events return true; } if (actionMasked == MotionEvent.ACTION_UP) { // Check if we are still within the same view if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) { super.dispatchTouchEvent(ev); } else { // Clear pressed state, cancel the action setPressed(false); invalidate(); return true; } } return super.dispatchTouchEvent(ev); } 

完整的自定义视图类可用: https : //gist.github.com/danosipov/6498490

让你的CustomListView

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

if condition孩子会处理触摸事件,确保你把你的if condition检查你需要滚动或不滚动。

对我来说最好的答案是丹奥西波夫的一个。 我会像这样改进它。 (发起取消动作事件而不是手动删除按下的状态)。

 @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK; if (actionMasked == MotionEvent.ACTION_DOWN) { // Record the position the list the touch landed on mPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); return super.dispatchTouchEvent(ev); } if (actionMasked == MotionEvent.ACTION_MOVE) { // Ignore move events return true; } if (actionMasked == MotionEvent.ACTION_UP) { // Check if we are still within the same view if (pointToPosition((int) ev.getX(), (int) ev.getY()) != mPosition) { // Clear pressed state, cancel the action ev.setAction(MotionEvent.ACTION_CANCEL); } } return super.dispatchTouchEvent(ev); } 

在列表视图中编写用于滑动删除的代码时,一旦检测到滑动,我想防止垂直滚动。 一旦滑动删除条件被满足,我在ACTION_MOVE部分设置一个布尔标志。 dispatchTouchEvent方法检查该条件并防止滚动工作。 在ACTION_UP中,我将标志设置为false以重新启用滚动。

 private float mY = Float.NaN; private boolean mStopScroll = false; @Override public boolean onTouch(View view, MotionEvent event) { if(!mStopScroll) { mY = event.getY(); } switch (event.getAction()) { case MotionEvent.ACTION_MOVE: if(<condition that stops the vertical scroll>) { mStopScroll = true; } break; case MotionEvent.ACTION_UP: mStopScroll = false; // your code here return true; default: } return false; } @Override public boolean dispatchTouchEvent(MotionEvent motionEvent) { if(mStopScroll) { motionEvent.setLocation(motionEvent.getX(), mY); } return super.dispatchTouchEvent(motionEvent); } 

对于Xamarin和MvvmCross的用户,我的回答会很有趣。 主要概念和以前的post一样,所以主要步骤是:

  1. 静音滚动事件
  2. dynamic更改列表高度

这里你是助手类,它允许禁用列表视图中的滚动:

 using System; using Cirrious.MvvmCross.Binding.Droid.Views; using Android.Content; using Android.Util; using Android.Views; using Android.Database; namespace MyProject.Android.Helpers { public class UnscrollableMvxListView : MvxListView { private MyObserver _myObserver; public UnscrollableMvxListView (Context context, IAttributeSet attrs, MvxAdapter adapter) : base(context, attrs, adapter) { } protected override void OnAttachedToWindow () { base.OnAttachedToWindow (); var dtso = new MyObserver(this); _myObserver = dtso; Adapter.RegisterDataSetObserver (dtso); } protected override void OnDetachedFromWindow () { Log.Debug ("UnscrollableMvxListView", "OnDetachedFromWindow"); if (_myObserver != null) { Adapter.UnregisterDataSetObserver (_myObserver); _myObserver = null; } base.OnDetachedFromWindow (); } //Make List Unscrollable private int _position; public override bool DispatchTouchEvent (MotionEvent ev) { MotionEventActions actionMasked = ev.ActionMasked & MotionEventActions.Mask; if (actionMasked == MotionEventActions.Down) { // Record the position the list the touch landed on _position = PointToPosition((int) ev.GetX (), (int) ev.GetY()); return base.DispatchTouchEvent(ev); } if (actionMasked == MotionEventActions.Move) { // Ignore move events return true; } if (actionMasked == MotionEventActions.Up) { // Check if we are still within the same view if (PointToPosition((int) ev.GetX(), (int) ev.GetY()) == _position) { base.DispatchTouchEvent(ev); } else { // Clear pressed state, cancel the action Pressed = false; Invalidate(); return true; } } return base.DispatchTouchEvent(ev); } //Make List Flat public void JustifyListViewHeightBasedOnChildren () { if (Adapter == null) { return; } var vg = this as ViewGroup; int totalHeight = 0; for (int i = 0; i < Adapter.Count; i++) { View listItem = Adapter.GetView(i, null, vg); listItem.Measure(0, 0); totalHeight += listItem.MeasuredHeight; } ViewGroup.LayoutParams par = LayoutParameters; par.Height = totalHeight + (DividerHeight * (Adapter.Count - 1)); LayoutParameters = par; RequestLayout(); } } internal class MyObserver : DataSetObserver { private readonly UnscrollableMvxListView _unscrollableMvxListView; public MyObserver (UnscrollableMvxListView lst) { _unscrollableMvxListView = lst; } public override void OnChanged() { Log.Debug("UnscrollableMvxListView", "OnChanged"); base.OnChanged (); _unscrollableMvxListView.JustifyListViewHeightBasedOnChildren (); } } } 

这是Joe Blow(OPpost评论)的代码,在http://danosipov.com/?p=604上指出,但是我在这里贴出来保存它,以防链接被孤立:;

 public class ScrollDisabledListView extends ListView { private int mPosition; public ScrollDisabledListView(Context context) { super(context); } public ScrollDisabledListView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK; if (actionMasked == MotionEvent.ACTION_DOWN) { // Record the position the list the touch landed on mPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); return super.dispatchTouchEvent(ev); } if (actionMasked == MotionEvent.ACTION_MOVE) { // Ignore move events return true; } if (actionMasked == MotionEvent.ACTION_UP) { // Check if we are still within the same view if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) { super.dispatchTouchEvent(ev); } else { // Clear pressed state, cancel the action setPressed(false); invalidate(); return true; } } return super.dispatchTouchEvent(ev); } } 

当您将此ListView添加到您的布局时,请记住在它的包名之前,否则当您尝试膨胀它时会引发exception。

尝试这个:

 listViewObject.setTranscriptMode(0); 

为我工作。