HorizontalScrollView within ScrollView Touch Handling

前端 未结 8 1801
轮回少年
轮回少年 2020-11-22 05:28

I have a ScrollView that surrounds my entire layout so that the entire screen is scrollable. The first element I have in this ScrollView is a HorizontalScrollView block tha

8条回答
  •  旧时难觅i
    2020-11-22 06:05

    I think I found a simpler solution, only this uses a subclass of ViewPager instead of (its parent) ScrollView.

    UPDATE 2013-07-16: I added an override for onTouchEvent as well. It could possibly help with the issues mentioned in the comments, although YMMV.

    public class UninterceptableViewPager extends ViewPager {
    
        public UninterceptableViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            boolean ret = super.onInterceptTouchEvent(ev);
            if (ret)
                getParent().requestDisallowInterceptTouchEvent(true);
            return ret;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            boolean ret = super.onTouchEvent(ev);
            if (ret)
                getParent().requestDisallowInterceptTouchEvent(true);
            return ret;
        }
    }
    

    This is similar to the technique used in android.widget.Gallery's onScroll(). It is further explained by the Google I/O 2013 presentation Writing Custom Views for Android.

    Update 2013-12-10: A similar approach is also described in a post from Kirill Grouchnikov about the (then) Android Market app.

提交回复
热议问题