Perfect solution ScrollView nested ScrollView Android slide in conflict

During the epidemic busy at home is really boring, uninteresting game, day by day, ate and slept, where can not go, thought she was pretty house, and I did not expect the new understanding of the epidemic myself, I realize I also have a yearning for freedom of the heart ah! Suddenly understand those who have long been shut out at home in the hope Sapo Gouzi is a kind of mood!

Bale Bale, holding the thought of leaving the computer used to write a lot of Demo, see if you can pick out a write (shui) blog post idea, so he had this article.

Slide conflict is a relatively common problem in Android, more complex pages are often encountered, especially in some of the effects, today I ScrollView nested ScrollView slide conflict, resolves the problem solving ideas, step by step to the actual code we explain, if where talking is not good, we also hope that the courage to shut up.

Problem Analysis

Tracing the source, the root causes of conflict arises in that slide slide is not executed correctly performer, sliding inside of the external part of hijacked consumed.

To understand this question we need to understand the event distribution mechanism in Android, this mechanism if the partners do not understand you can visit this blog - the package you understand Android event distribution mechanism

So, we look for in ScrollView slip events is how to do treatment.

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        
        /*
        * Shortcut the most recurring case: the user is in the dragging
        * state and he is moving his finger.  We want to intercept this
        * motion.
        * 当用户手指在屏幕上处于滑动状态时,拦截这个动作。
        */
        final int action = ev.getAction();
        if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
            return true;
        }

        /*...*/
}

See not, reasoned, this pot ScrollView himself back up.

Solutions

1, the internal control slide obtain

So I want to ask you about you in front of the computer and found the reason after you know how to solve it? In fact, very simple, do not let it control the outer part of the inner sliding interception just fine, then how to do it, I found the ScrollView onInterceptTouchEvent () and no operation when the user's finger pressed to intercept, that is no MotionEvent.ACTION_DOWN intercept, which means that when the user pressed a finger, this action can be transmitted to the inner ScrollVIew in (otherwise ScrollView click View in other events how long press response) on the other VIew, then we can re write a ScrollView, the outer layer does not request an operation to intercept when it receives MotionEvent.ACTION_DOWN this action. This is important, because when the internal ScrollVIew grasp the initiative, a series of operations can handle the user's finger on the screen,

2, the inner and outer sliding correlation

Well, here it is to resolve all? No, no, this is just the beginning, if you follow me at this time above ideas Qiaowan code, you will find both internal and external ScrollVIew slip events are independent, although internal ScrollView can slide, but when it slides to the top or bottom and when there is no linkage with external ScrollView, sliding experience so that users will be poor, and how to solve it? Start your clever little head think, to argue, it is the right time to intercept it right back to the outer layer. So what is it the right time? When ScrollView nothing more than an internal slide the top or bottom.

OK, the problem is clear, the program also provides, now you can try to write the code to try to resolve the conflict.

The actual code

Here I put the code I write out for your reference.

public class MyScrollView extends ScrollView {
    
    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context) {
        super(context);
    }

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

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

    //在Y轴上可以滑动的最大距离 = 总长度 - 当前展示区域长度
    private int maxY = getChildAt(0).getMeasuredHeight() - getHeight();
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                getParent().getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                if (getScrollY() > 0 && getScrollY() < maxY)
                    getParent().requestDisallowInterceptTouchEvent(true);
                else
                    getParent().requestDisallowInterceptTouchEvent(false);
                /*if (getScrollY()==0)
                    Log.i("kkk","滑到头了");
                if (getChildAt(0).getMeasuredHeight() == getScrollY() + getHeight())
                    Log.i("kkk","滑到底了");*/
                break;
            case MotionEvent.ACTION_UP:
                getParent().getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}

Rewritten nested sliding effect ScrollView

At last

When I came into contact with the development of Android, the issue of conflict of such events is that nothing, because they do not know how to troubleshoot the problem, do not know how to start, but with the deepening of the knowledge of Android, far as to the look at these issues, but so, life is it not it so, regardless of the past or the future, we do not know the question has always waiting for us to solve, and we continue to be addressed in the course of progress and growth, achievement better self. For this year's burst of new coronavirus has not only our personal problems to face, but we all need to face the Chinese people to solve problems, and sincerely hope that this epidemic can go over to wish those anti Phytophthora front-line staff can take care of themselves, you are the most handsome of the most beautiful! Go China! ! !

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

Guess you like

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