Android is the perfect solution ScrollView nested ListView slide conflict

Before writing a how to solve the problem ScrollView nested ScrollView slide conflict, because it is the same problem and the nature of this conflict causes of the slide caused by different nesting, Solutions are also the same, the only difference backward in judgment on specific conditions , so this post on the code, and want to see the cause of the problem solving ideas can view this article - the perfect solution for Android in ScrollView nested ScrollView slide conflict

public class MyScrollListView extends ListView {
    public MyScrollListView(Context context) {
        super(context);
    }

    public MyScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    float preY = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                preY = ev.getY();
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                if (slideToTheTop(ev) || slideToTheBottom(ev))
                    getParent().requestDisallowInterceptTouchEvent(false);
                else
                    getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 当第一个可见item为0且手势为向下滑动且全部露出
     * @param ev
     * @return
     */
    private boolean slideToTheTop(MotionEvent ev) {
        return getFirstVisiblePosition() == 0 && getChildAt(0).getTop() == 0 && ev.getY() - preY > 0;
    }

    /**
     * 最后一个可见item为全部item最后一个且手势向上滑动且全部露出
     * @param ev
     * @return
     */
    private boolean slideToTheBottom(MotionEvent ev) {
        return getLastVisiblePosition() == getCount() - 1 && getChildAt(getChildCount() - 1).getBottom() == getHeight() && ev.getY() - preY < 0;
    }
}

 

Published 57 original articles · won praise 26 · views 40000 +

Guess you like

Origin blog.csdn.net/Ever69/article/details/104343216