Custom onInterceptTouchEvent in ListView

流过昼夜 提交于 2020-01-02 06:45:32

问题


How can I implement a custom onInterceptTouchEvent() in a ListView that give the scrolling priority to the child's of the ListView and as soon as they did their scrolling , give it back to the ListView ? I want to give priority to the inner views.


回答1:


Try overriding onInterceptTouchEvent() of your children like this:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(!isAtTop && !isAtBottom){
        getParent().requestDisallowInterceptTouchEvent(true);   
    }             
    return super.onInterceptTouchEvent(ev);
} 

In onInterceptTouchEvent() calculate if the ListView has scrolled totally to the top or bottom. If it is somewhere in between then ask the parent to not intercept touches.

To check for top or bottom try:

int scrollRange = computeVerticalScrollRange();
int scrollOffset = computeVerticalScrollOffset();
int scrollExtend = computeVerticalScrollExtent();
if(scrollOffset == 0){
    //AtTop
}else if(scrollRange == scrollOffset + scrollExtend){
    //AtBottom
}


来源:https://stackoverflow.com/questions/17719787/custom-onintercepttouchevent-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!