zoukankan      html  css  js  c++  java
  • 安卓开发37:自定义的HorizontalScrollView类,使其pageScroll的时候焦点不选中

    自定义一个HorizontalScrollView类,主要为了让这个HorizontalScrollView不能鼠标点击,不能左右按键,并且没有焦点。


    public class ImageMoveHorizontalScrollView extends HorizontalScrollView {
    
        private boolean    mSmoothScrollingEnabled = true;
    
        private final Rect mTempRect               = new Rect();
    
        public ImageMoveHorizontalScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ImageMoveHorizontalScrollView(Context context) {
            super(context);
        }
    
        /**
         * 关闭鼠标点击的效果,重写该方法
         */
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return false;
        }
    
        /**
         * 关闭左右按键效果,重写该方法
         */
        @Override
        public boolean executeKeyEvent(KeyEvent event) {
            return false;
        }
    
        /**
         * 去除焦点选中
         */
        @Override
        public boolean pageScroll(int direction) {
            boolean right = direction == View.FOCUS_RIGHT;
            int width = getWidth();
    
            if (right) {
                mTempRect.left = getScrollX() + width;
                int count = getChildCount();
                if (count > 0) {
                    View view = getChildAt(0);
                    if (mTempRect.left + width > view.getRight()) {
                        mTempRect.left = view.getRight() - width;
                    }
                }
            } else {
                mTempRect.left = getScrollX() - width;
                if (mTempRect.left < 0) {
                    mTempRect.left = 0;
                }
            }
            mTempRect.right = mTempRect.left + width;
    
            return scrollAndFocus(direction, mTempRect.left, mTempRect.right);
        }
    
        private boolean scrollAndFocus(int direction, int left, int right) {
            boolean handled = true;
    
            int width = getWidth();
            int containerLeft = getScrollX();
            int containerRight = containerLeft + width;
            boolean goLeft = direction == View.FOCUS_LEFT;
    //主要在这边,注释掉下面的代码
            //        View newFocused = findFocusableViewInBounds(goLeft, left, right);
            //        if (newFocused == null) {
            //            newFocused = this;
            //        }
    
            if (left >= containerLeft && right <= containerRight) {
                handled = false;
            } else {
                int delta = goLeft ? (left - containerLeft) : (right - containerRight);
                doScrollX(delta);
            }
            //去除 滚动后 foucus的
            //        if (newFocused != findFocus())
            //            newFocused.requestFocus(direction);
    
            return handled;
        }
    
        /**
         * Smooth scroll by a X delta
         * @param delta the number of pixels to scroll by on the X axis
         */
        private void doScrollX(int delta) {
            if (delta != 0) {
                if (mSmoothScrollingEnabled) {
                    smoothScrollBy(delta, 0);
                } else {
                    scrollBy(delta, 0);
                }
            }
        }
    
    }
    


    使用:

     sc.pageScroll(View.FOCUS_RIGHT);  //向右翻一页


  • 相关阅读:
    CSS网站变灰
    长列表优化之滚动替换数据方案小记
    JS把数组中相同元素组合成一个新的数组问题
    yahoo CSS reset
    IE调试网页之三:使用 F12 工具控制台查看错误和状态 (Windows)
    KMP算法的JavaScript实现
    Android系统版本SDK_INT与版本对应关系
    利用jQuery的deferred异步按顺序加载JS文件
    Javascript图像处理之平滑处理
    IE调试网页之七:使用探查器工具分析代码性能 (Windows)
  • 原文地址:https://www.cnblogs.com/james1207/p/3317974.html
Copyright © 2011-2022 走看看