Android 在listview中禁用滚动
我有一个列表视图,根据一些逻辑,我想暂时禁用滚动。 view.setOnScrollListener(空);我想我需要写一些代码,有人能给我一个历史或一些片段吗Android 在listview中禁用滚动,android,listview,scroll,listener,Android,Listview,Scroll,Listener,我有一个列表视图,根据一些逻辑,我想暂时禁用滚动。 view.setOnScrollListener(空);我想我需要写一些代码,有人能给我一个历史或一些片段吗 谢谢制作您的自定义列表视图 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(needToStop){ return false;} return super.onInterceptTouchEvent(ev); } 在fals
谢谢制作您的
自定义列表视图
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(needToStop){
return false;}
return super.onInterceptTouchEvent(ev);
}
在
false
上,孩子们将处理触摸事件,确保您输入if条件来检查您是否需要滚动。在您的CustomListView中:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction()==MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
然后ListView将对单击做出反应,但不会更改滚动位置。不创建新的自定义ListView的另一个选项是将onTouchListener
附加到ListView,如果运动事件动作是动作
,则在onTouch()回调中返回true
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
如果将事件绑定到列表项,则拖动包含这些解决方案的列表仍将触发该事件。您希望使用以下方法来说明用户希望通过从所选项目(根据指针Null的答案改编)拖动来取消事件:
完整的自定义视图类可用:在列表视图上编写要删除的滑动代码时,我希望在检测到滑动后防止垂直滚动。一旦满足滑动删除条件,我就在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);
}
private float mY=float.NaN;
私有布尔值mStopScroll=false;
@凌驾
公共布尔onTouch(视图、运动事件){
如果(!mStopScroll){
mY=event.getY();
}
开关(event.getAction()){
case MotionEvent.ACTION\u移动:
if(){
mStopScroll=true;
}
打破
case MotionEvent.ACTION\u UP:
mStopScroll=false;
//你的代码在这里
返回true;
违约:
}
返回false;
}
@凌驾
公共布尔dispatchTouchEvent(MotionEvent MotionEvent){
如果(mStopScroll){
setLocation(motionEvent.getX(),mY);
}
返回super.dispatchTouchEvent(motionEvent);
}
我的答案对Xamarin和MvvmCross用户来说很有意思。主要概念与之前的文章相同,因此主要步骤如下:
静音滚动事件
动态更改列表高度
这里是帮助器类,允许在列表视图中禁用滚动:
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 ();
}
}
}
使用系统;
使用cirries.MvvmCross.Binding.Droid.Views;
使用Android.Content;
使用Android.Util;
使用Android.Views;
使用Android.Database;
命名空间MyProject.Android.Helpers
{
公共类可展开MVXListView
:MvxListView
{
专用MyObserver_MyObserver;
公共可反汇编MVXListView(上下文上下文、IAttributeSet属性、MVXAAdapter适配器)
:base(上下文、属性、适配器)
{
}
受保护的覆盖无效的附加DTOWINDOW()
{
base.OnAttachedToWindow();
var dtso=新的MyObserver(此);
_myObserver=dtso;
Adapter.RegisterDataSetObserver(dtso);
}
受保护的覆盖无效OnDetachedFromWindow()
{
调试(“UncollableMVXListView”,“OnDetachedFromWindow”);
if(_myObserver!=null){
Adapter.UnregisterDataSetObserver(_myObserver);
_myObserver=null;
}
base.OnDetachedFromWindow();
}
//使列表不可滚动
私人国际职位;
公共覆盖bool DispatchTouchEvent(MotionEvent ev)
{
MotionEventActions actionMasked=ev.actionMasked&MotionEventActions.Mask;
if(actionMasked==MotionEventActions.Down){
//记录触摸屏落在列表上的位置
_位置=点位置((int)ev.GetX(),(int)ev.GetY());
返回基地。DispatchTouchEvent(ev);
}
if(actionMasked==MotionEventActions.Move){
//忽略移动事件
返回true;
}
if(actionMasked==MotionEventActions.Up){
//检查我们是否仍在同一视图中
如果(点位置((int)ev.GetX(),(int)ev.GetY())=\u位置){
基本。DispatchTouchEvent(ev);
}否则{
//清除按下状态,取消操作
按下=假;
使无效();
返回true;
}
}
返回基地。DispatchTouchEvent(ev);
}
//使列表扁平化
public void JustifyListViewHeightBasedOnChildren(){
if(适配器==null){
返回;
}
var vg=此为视图组;
int totalHeight=0;
for(int i=0;i
最好的answ
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 ();
}
}
}
@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);
}
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);
}
}
listViewObject.setTranscriptMode(0);