zoukankan      html  css  js  c++  java
  • Android 实现ListView不可滚动效果


        希望得到的效果是ListView不能滚动,可是最大的问题在与ListView Item还必有点击事件。假设不须要点击事件那就简单了,直接设置ListView.setEnable(false);

        假设还须要点击事件。滚动与点击都是在ListView Touch处理机制管理。

        ListView点击事件是复用ViewGroup的处理逻辑,当用户点击视图而且按下与抬起手指之间移动距离非常小,满足点击事件的时间长度限制,就会触发点击事件。

        ListView滚动事件是自己处理。有两个推断条件,当用户触发move事件而且滑动超过touch slop距离 或者 滑动速度超过阀值都会判定为滚动事件。


    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.ListView;
    
    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) {
                // 记录手指按下时的位置
                mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
                return super.dispatchTouchEvent(ev);
            }
     
            if (actionMasked == MotionEvent.ACTION_MOVE) {
                // 最关键的地方,忽略MOVE 事件
            	// ListView onTouch获取不到MOVE事件所以不会发生滚动处理
                return true;
            }
     
            // 手指抬起时
            if (actionMasked == MotionEvent.ACTION_UP
            		|| actionMasked == MotionEvent.ACTION_CANCEL) {
                // 手指按下与抬起都在同一个视图内。交给父控件处理,这是一个点击事件
                if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
                    super.dispatchTouchEvent(ev);
                } else {
                	// 假设手指已经移出按下时的Item,说明是滚动行为,清理Item pressed状态
                    setPressed(false);
                    invalidate();
                    return true;
                }
            }
     
            return super.dispatchTouchEvent(ev);
        }
    }

    參考资料:

    Disable scrolling in Android ListView



    转载请注明出处:

    Android 设置ListView不可滚动 

    http://blog.csdn.net/androiddevelop/article/details/38815493




  • 相关阅读:
    NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接
    Myeclipse闪退故障
    Log4j快速使用精简版
    Eclipse快捷键 10个最有用的快捷键
    Java compiler level does not match解决方法
    ArcMap常用VBA
    firefox浏览器中silverlight无法输入问题
    C#导入Excel遇到数字字母混合列数据丢失解决
    ArcMap计算PolyLine中点VBA
    Apple Watch 开发详解
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5135380.html
Copyright © 2011-2022 走看看