Scrollview嵌套HorizontalScrollView導致橫向滑動卡頓現象解決

開發中經驗會遇到滑動裏面嵌入滑動的問題,但是這種情況下觸摸事件就會發生衝突。導致滑動非常卡,甚至出現程序停止響應。這種情況下我們一般需要重寫view。下面給出重新scrollview的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class CustomScrollView extends ScrollView {
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;
  
    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        setFadingEdgeLength(0);
    }
  
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }
  
    // Return false if we're scrolling in the x direction  
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }
}

使用的時候使用這個自定義的控件就可以了。

轉載原創文章請註明,轉載自:IT驛站[http://www.blogchen.com]

本文鏈接: http://www.blogchen.com/archives/584.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章